Skip to content

Commit 8ab9252

Browse files
committed
DB: Added extra query tests, updated db-testing scripts
Also added skipping to avif tests for environments where GD does not have built-in AVIF support
1 parent befc645 commit 8ab9252

File tree

4 files changed

+69
-15
lines changed

4 files changed

+69
-15
lines changed

app/Entities/Models/Book.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ public function directPages(): HasMany
6767
*/
6868
public function chapters(): HasMany
6969
{
70-
return $this->hasMany(Chapter::class)
71-
->where('type', '=', 'chapter');
70+
return $this->hasMany(Chapter::class);
7271
}
7372

7473
/**

dev/docker/db-testing/run.sh

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@ BRANCH=${1:-development}
44

55
# Build the container with a known name
66
docker build --build-arg BRANCH="$BRANCH" -t bookstack:db-testing .
7+
if [ $? -eq 1 ]; then
8+
echo "Failed to build app container for testing"
9+
exit 1
10+
fi
711

812
# List of database containers to test against
913
containers=(
10-
mysql:5.7
11-
mysql:8.0
12-
mysql:8.4
13-
mysql:9.5
14-
mariadb:10.2
15-
mariadb:10.6
16-
mariadb:10.11
17-
mariadb:11.4
18-
mariadb:11.8
19-
mariadb:12.0
14+
"mysql:5.7"
15+
"mysql:8.0"
16+
"mysql:8.4"
17+
"mysql:9.5"
18+
"mariadb:10.2"
19+
"mariadb:10.6"
20+
"mariadb:10.11"
21+
"mariadb:11.4"
22+
"mariadb:11.8"
23+
"mariadb:12.0"
2024
)
2125

2226
# Pre-clean-up from prior runs
@@ -28,10 +32,13 @@ for img in "${containers[@]}"; do
2832
echo "Starting tests with $img..."
2933
docker network create bs-dbtest-net
3034
docker run -d --rm --name "bs-dbtest-db" \
31-
-e MYSQL_DATABASE=bookstack-test -e MYSQL_USER=bookstack -e MYSQL_PASSWORD=bookstack -e MYSQL_ROOT_PASSWORD=password \
35+
-e MYSQL_DATABASE=bookstack-test \
36+
-e MYSQL_USER=bookstack \
37+
-e MYSQL_PASSWORD=bookstack \
38+
-e MYSQL_ROOT_PASSWORD=password \
3239
--network=bs-dbtest-net \
33-
"$img"
34-
sleep 10
40+
"$img"
41+
sleep 20
3542
APP_RUN='docker run -it --rm --network=bs-dbtest-net -e TEST_DATABASE_URL="mysql://bookstack:bookstack@bs-dbtest-db:3306" bookstack:db-testing'
3643
$APP_RUN artisan migrate --force --database=mysql_testing
3744
$APP_RUN artisan db:seed --force --class=DummyContentSeeder --database=mysql_testing

tests/Entity/EntityQueryTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Entity;
4+
5+
use BookStack\Entities\Models\Book;
6+
use Illuminate\Database\Eloquent\Builder;
7+
use Tests\TestCase;
8+
9+
class EntityQueryTest extends TestCase
10+
{
11+
public function test_basic_entity_query_has_join_and_type_applied()
12+
{
13+
$query = Book::query();
14+
$expected = 'select * from `entities` left join `entity_container_data` on `entity_container_data`.`entity_id` = `entities`.`id` and `entity_container_data`.`entity_type` = ? where `type` = ? and `entities`.`deleted_at` is null';
15+
$this->assertEquals($expected, $query->toSql());
16+
$this->assertEquals(['book', 'book'], $query->getBindings());
17+
}
18+
19+
public function test_joins_in_sub_queries_use_alias_names()
20+
{
21+
$query = Book::query()->whereHas('chapters', function (Builder $query) {
22+
$query->where('name', '=', 'a');
23+
});
24+
25+
// Probably from type limits on relation where not needed?
26+
$expected = 'select * from `entities` left join `entity_container_data` on `entity_container_data`.`entity_id` = `entities`.`id` and `entity_container_data`.`entity_type` = ? where exists (select * from `entities` as `laravel_reserved_%d` left join `entity_container_data` on `entity_container_data`.`entity_id` = `laravel_reserved_%d`.`id` and `entity_container_data`.`entity_type` = ? where `entities`.`id` = `laravel_reserved_%d`.`book_id` and `name` = ? and `type` = ? and `laravel_reserved_%d`.`deleted_at` is null) and `type` = ? and `entities`.`deleted_at` is null';
27+
$this->assertStringMatchesFormat($expected, $query->toSql());
28+
$this->assertEquals(['book', 'chapter', 'a', 'chapter', 'book'], $query->getBindings());
29+
}
30+
31+
public function test_book_chapter_relation_applies_type_condition()
32+
{
33+
$book = $this->entities->book();
34+
$query = $book->chapters();
35+
$expected = 'select * from `entities` left join `entity_container_data` on `entity_container_data`.`entity_id` = `entities`.`id` and `entity_container_data`.`entity_type` = ? where `entities`.`book_id` = ? and `entities`.`book_id` is not null and `type` = ? and `entities`.`deleted_at` is null';
36+
$this->assertEquals($expected, $query->toSql());
37+
$this->assertEquals(['chapter', $book->id, 'chapter'], $query->getBindings());
38+
39+
$query = Book::query()->whereHas('chapters');
40+
$expected = 'select * from `entities` left join `entity_container_data` on `entity_container_data`.`entity_id` = `entities`.`id` and `entity_container_data`.`entity_type` = ? where exists (select * from `entities` as `laravel_reserved_%d` left join `entity_container_data` on `entity_container_data`.`entity_id` = `laravel_reserved_%d`.`id` and `entity_container_data`.`entity_type` = ? where `entities`.`id` = `laravel_reserved_%d`.`book_id` and `type` = ? and `laravel_reserved_%d`.`deleted_at` is null) and `type` = ? and `entities`.`deleted_at` is null';
41+
$this->assertStringMatchesFormat($expected, $query->toSql());
42+
$this->assertEquals(['book', 'chapter', 'chapter', 'book'], $query->getBindings());
43+
}
44+
}

tests/Uploads/ImageTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public function test_image_display_thumbnail_generation_for_apng_images_uses_ori
7373

7474
public function test_image_display_thumbnail_generation_for_animated_avif_images_uses_original_file()
7575
{
76+
if (! function_exists('imageavif')) {
77+
$this->markTestSkipped('imageavif() is not available');
78+
}
79+
7680
$page = $this->entities->page();
7781
$admin = $this->users->admin();
7882
$this->actingAs($admin);

0 commit comments

Comments
 (0)