1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-11-11 22:02:36 +03:00

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
This commit is contained in:
Dan Brown
2025-11-11 11:23:16 +00:00
parent befc645705
commit 8ab9252f9b
4 changed files with 69 additions and 15 deletions

View File

@@ -67,8 +67,7 @@ class Book extends Entity implements HasDescriptionInterface, HasCoverInterface,
*/ */
public function chapters(): HasMany public function chapters(): HasMany
{ {
return $this->hasMany(Chapter::class) return $this->hasMany(Chapter::class);
->where('type', '=', 'chapter');
} }
/** /**

View File

@@ -4,19 +4,23 @@ BRANCH=${1:-development}
# Build the container with a known name # Build the container with a known name
docker build --build-arg BRANCH="$BRANCH" -t bookstack:db-testing . docker build --build-arg BRANCH="$BRANCH" -t bookstack:db-testing .
if [ $? -eq 1 ]; then
echo "Failed to build app container for testing"
exit 1
fi
# List of database containers to test against # List of database containers to test against
containers=( containers=(
mysql:5.7 "mysql:5.7"
mysql:8.0 "mysql:8.0"
mysql:8.4 "mysql:8.4"
mysql:9.5 "mysql:9.5"
mariadb:10.2 "mariadb:10.2"
mariadb:10.6 "mariadb:10.6"
mariadb:10.11 "mariadb:10.11"
mariadb:11.4 "mariadb:11.4"
mariadb:11.8 "mariadb:11.8"
mariadb:12.0 "mariadb:12.0"
) )
# Pre-clean-up from prior runs # Pre-clean-up from prior runs
@@ -28,10 +32,13 @@ for img in "${containers[@]}"; do
echo "Starting tests with $img..." echo "Starting tests with $img..."
docker network create bs-dbtest-net docker network create bs-dbtest-net
docker run -d --rm --name "bs-dbtest-db" \ docker run -d --rm --name "bs-dbtest-db" \
-e MYSQL_DATABASE=bookstack-test -e MYSQL_USER=bookstack -e MYSQL_PASSWORD=bookstack -e MYSQL_ROOT_PASSWORD=password \ -e MYSQL_DATABASE=bookstack-test \
-e MYSQL_USER=bookstack \
-e MYSQL_PASSWORD=bookstack \
-e MYSQL_ROOT_PASSWORD=password \
--network=bs-dbtest-net \ --network=bs-dbtest-net \
"$img" "$img"
sleep 10 sleep 20
APP_RUN='docker run -it --rm --network=bs-dbtest-net -e TEST_DATABASE_URL="mysql://bookstack:bookstack@bs-dbtest-db:3306" bookstack:db-testing' APP_RUN='docker run -it --rm --network=bs-dbtest-net -e TEST_DATABASE_URL="mysql://bookstack:bookstack@bs-dbtest-db:3306" bookstack:db-testing'
$APP_RUN artisan migrate --force --database=mysql_testing $APP_RUN artisan migrate --force --database=mysql_testing
$APP_RUN artisan db:seed --force --class=DummyContentSeeder --database=mysql_testing $APP_RUN artisan db:seed --force --class=DummyContentSeeder --database=mysql_testing

View File

@@ -0,0 +1,44 @@
<?php
namespace Entity;
use BookStack\Entities\Models\Book;
use Illuminate\Database\Eloquent\Builder;
use Tests\TestCase;
class EntityQueryTest extends TestCase
{
public function test_basic_entity_query_has_join_and_type_applied()
{
$query = Book::query();
$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';
$this->assertEquals($expected, $query->toSql());
$this->assertEquals(['book', 'book'], $query->getBindings());
}
public function test_joins_in_sub_queries_use_alias_names()
{
$query = Book::query()->whereHas('chapters', function (Builder $query) {
$query->where('name', '=', 'a');
});
// Probably from type limits on relation where not needed?
$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';
$this->assertStringMatchesFormat($expected, $query->toSql());
$this->assertEquals(['book', 'chapter', 'a', 'chapter', 'book'], $query->getBindings());
}
public function test_book_chapter_relation_applies_type_condition()
{
$book = $this->entities->book();
$query = $book->chapters();
$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';
$this->assertEquals($expected, $query->toSql());
$this->assertEquals(['chapter', $book->id, 'chapter'], $query->getBindings());
$query = Book::query()->whereHas('chapters');
$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';
$this->assertStringMatchesFormat($expected, $query->toSql());
$this->assertEquals(['book', 'chapter', 'chapter', 'book'], $query->getBindings());
}
}

View File

@@ -73,6 +73,10 @@ class ImageTest extends TestCase
public function test_image_display_thumbnail_generation_for_animated_avif_images_uses_original_file() public function test_image_display_thumbnail_generation_for_animated_avif_images_uses_original_file()
{ {
if (! function_exists('imageavif')) {
$this->markTestSkipped('imageavif() is not available');
}
$page = $this->entities->page(); $page = $this->entities->page();
$admin = $this->users->admin(); $admin = $this->users->admin();
$this->actingAs($admin); $this->actingAs($admin);