Skip to content

Commit 8082c95

Browse files
committed
Merge branch 'development' into release
2 parents fcabf47 + 8ab9252 commit 8082c95

File tree

6 files changed

+140
-5
lines changed

6 files changed

+140
-5
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
/**

app/Entities/Models/EntityScope.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ class EntityScope implements Scope
1515
public function apply(Builder $builder, Model $model): void
1616
{
1717
$builder = $builder->where('type', '=', $model->getMorphClass());
18+
$table = $model->getTable();
1819
if ($model instanceof Page) {
19-
$builder->leftJoin('entity_page_data', 'entity_page_data.page_id', '=', 'entities.id');
20+
$builder->leftJoin('entity_page_data', 'entity_page_data.page_id', '=', "{$table}.id");
2021
} else {
21-
$builder->leftJoin('entity_container_data', function (JoinClause $join) use ($model) {
22-
$join->on('entity_container_data.entity_id', '=', 'entities.id')
22+
$builder->leftJoin('entity_container_data', function (JoinClause $join) use ($model, $table) {
23+
$join->on('entity_container_data.entity_id', '=', "{$table}.id")
2324
->where('entity_container_data.entity_type', '=', $model->getMorphClass());
2425
});
2526
}

dev/docker/db-testing/Dockerfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
FROM ubuntu:24.04
2+
3+
# Install additional dependencies
4+
RUN apt-get update && \
5+
apt-get install -y \
6+
git \
7+
wget \
8+
zip \
9+
unzip \
10+
php \
11+
php-bcmath php-curl php-mbstring php-gd php-xml php-zip php-mysql php-ldap \
12+
&& \
13+
rm -rf /var/lib/apt/lists/*
14+
15+
# Take branch as an argument so we can choose which BookStack
16+
# branch to test against
17+
ARG BRANCH=development
18+
19+
# Download BookStack & install PHP deps
20+
RUN mkdir -p /var/www && \
21+
git clone https://github.com/bookstackapp/bookstack.git --branch "$BRANCH" --single-branch /var/www/bookstack && \
22+
cd /var/www/bookstack && \
23+
wget https://raw.githubusercontent.com/composer/getcomposer.org/f3108f64b4e1c1ce6eb462b159956461592b3e3e/web/installer -O - -q | php -- --quiet --filename=composer && \
24+
php composer install
25+
26+
# Set the BookStack dir as the default working dir
27+
WORKDIR /var/www/bookstack
28+
29+
# Set the default action as running php
30+
ENTRYPOINT ["/bin/php"]

dev/docker/db-testing/run.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
3+
BRANCH=${1:-development}
4+
5+
# Build the container with a known name
6+
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
11+
12+
# List of database containers to test against
13+
containers=(
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"
24+
)
25+
26+
# Pre-clean-up from prior runs
27+
docker stop bs-dbtest-db
28+
docker network rm bs-dbtest-net
29+
30+
# Cycle over each database image
31+
for img in "${containers[@]}"; do
32+
echo "Starting tests with $img..."
33+
docker network create bs-dbtest-net
34+
docker run -d --rm --name "bs-dbtest-db" \
35+
-e MYSQL_DATABASE=bookstack-test \
36+
-e MYSQL_USER=bookstack \
37+
-e MYSQL_PASSWORD=bookstack \
38+
-e MYSQL_ROOT_PASSWORD=password \
39+
--network=bs-dbtest-net \
40+
"$img"
41+
sleep 20
42+
APP_RUN='docker run -it --rm --network=bs-dbtest-net -e TEST_DATABASE_URL="mysql://bookstack:bookstack@bs-dbtest-db:3306" bookstack:db-testing'
43+
$APP_RUN artisan migrate --force --database=mysql_testing
44+
$APP_RUN artisan db:seed --force --class=DummyContentSeeder --database=mysql_testing
45+
$APP_RUN vendor/bin/phpunit
46+
if [ $? -eq 0 ]; then
47+
echo "$img - Success"
48+
else
49+
echo "$img - Error"
50+
read -p "Stop script? [y/N] " ans
51+
[[ $ans == [yY] ]] && exit 0
52+
fi
53+
54+
docker stop "bs-dbtest-db"
55+
docker network rm bs-dbtest-net
56+
done
57+

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)