From 9c0e70e3653085f3e593eedfcb295972bc08645b Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 15 Nov 2023 17:02:08 +0100 Subject: [PATCH 1/9] Fix & tests for #284 --- src/utilities/ScoutUtility.php | 8 ++++++-- tests/_craft/config/db.php | 6 +++--- tests/docker-compose.yml | 14 -------------- tests/unit/ScoutUtilityTest.php | 6 +++++- 4 files changed, 14 insertions(+), 20 deletions(-) delete mode 100644 tests/docker-compose.yml diff --git a/src/utilities/ScoutUtility.php b/src/utilities/ScoutUtility.php index 8d6748d..b28a064 100644 --- a/src/utilities/ScoutUtility.php +++ b/src/utilities/ScoutUtility.php @@ -41,8 +41,12 @@ public static function contentHtml(): string $sites = 'all'; if ($engine->scoutIndex->criteria->siteId != '*') { $sites = []; - foreach ($engine->scoutIndex->criteria->siteId as $id) { - $sites[] = Craft::$app->getSites()->getSiteById($id); + if (is_array($engine->scoutIndex->criteria->siteId)) { + foreach ($engine->scoutIndex->criteria->siteId as $id) { + $sites[] = Craft::$app->getSites()->getSiteById($id); + } + } else { + $sites = $engine->scoutIndex->criteria->siteId; } } $stats = array_merge($stats, [ diff --git a/tests/_craft/config/db.php b/tests/_craft/config/db.php index 89e9d29..ae03391 100644 --- a/tests/_craft/config/db.php +++ b/tests/_craft/config/db.php @@ -1,12 +1,12 @@ 'root', - 'user' => 'root', + 'password' => 'db', + 'user' => 'db', 'database' => 'scout_testing', 'tablePrefix' => '', 'driver' => 'mysql', 'port' => 3306, 'schema' => getenv('DB_SCHEMA'), - 'server' => '127.0.0.1', + 'server' => 'db', ]; diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml deleted file mode 100644 index 5582476..0000000 --- a/tests/docker-compose.yml +++ /dev/null @@ -1,14 +0,0 @@ -version: "2" -services: - mysql: - restart: always - image: mysql/mysql-server:5.7 - environment: - MYSQL_ROOT_PASSWORD: "root_password" - MYSQL_DATABASE: "scout_testing" - MYSQL_USER: "root" - MYSQL_PASSWORD: "root" - MYSQL_ROOT_HOST: "0.0.0.0" - MYSQL_ALLOW_EMPTY_PASSWORD: "yes" - ports: - - "3306:3306" diff --git a/tests/unit/ScoutUtilityTest.php b/tests/unit/ScoutUtilityTest.php index 5e9b24f..0501f88 100644 --- a/tests/unit/ScoutUtilityTest.php +++ b/tests/unit/ScoutUtilityTest.php @@ -27,9 +27,13 @@ protected function _before() 'engine' => FakeEngine::class, 'indices' => [ ScoutIndex::create('Blog') - ->criteria(function($query) { + ->criteria(function ($query) { return $query; }), + ScoutIndex::create('Blog_with_one_site') + ->criteria(function ($query) { + return $query->siteId(1); + }), ], ]); $scout->init(); From f84a86035f2fca63fe5430d597798aa94846c71c Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 15 Nov 2023 19:36:18 +0100 Subject: [PATCH 2/9] Use env file to configure tests --- tests/_bootstrap.php | 6 +++++- tests/_craft/config/db.php | 12 ++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/tests/_bootstrap.php b/tests/_bootstrap.php index a0d5de0..1a32009 100644 --- a/tests/_bootstrap.php +++ b/tests/_bootstrap.php @@ -20,7 +20,11 @@ // Load dotenv? if (file_exists(CRAFT_BASE_PATH . '/.env')) { - (new Dotenv\Dotenv(CRAFT_BASE_PATH))->load(); + if (class_exists(Dotenv\Dotenv::class)) { + // By default, this will allow .env file values to override environment variables + // with matching names. Use `createUnsafeImmutable` to disable this. + Dotenv\Dotenv::createUnsafeMutable(CRAFT_BASE_PATH)->safeLoad(); + } } // Load and run Craft diff --git a/tests/_craft/config/db.php b/tests/_craft/config/db.php index ae03391..8a608fc 100644 --- a/tests/_craft/config/db.php +++ b/tests/_craft/config/db.php @@ -1,12 +1,12 @@ 'db', - 'user' => 'db', - 'database' => 'scout_testing', + 'server' => getenv('DB_SERVER') ?? 'db', + 'database' => getenv('DB_NAME') ?? 'scout_testing', + 'user' => getenv('DB_USER') ?? 'db', + 'password' => getenv('DB_PASSWORD') ?? 'db', + 'schema' => getenv('DB_SCHEMA'), 'tablePrefix' => '', 'driver' => 'mysql', - 'port' => 3306, - 'schema' => getenv('DB_SCHEMA'), - 'server' => 'db', + 'port' => getenv('DB_PORT') ?? 3306, ]; From a0e53eb98cab2880adb77de5655e1976c034b864 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 15 Nov 2023 19:42:06 +0100 Subject: [PATCH 3/9] DB config for tests? --- tests/_craft/config/db.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/_craft/config/db.php b/tests/_craft/config/db.php index 8a608fc..20481e0 100644 --- a/tests/_craft/config/db.php +++ b/tests/_craft/config/db.php @@ -1,10 +1,10 @@ getenv('DB_SERVER') ?? 'db', + 'server' => getenv('DB_SERVER') ?? '127.0.0.1', 'database' => getenv('DB_NAME') ?? 'scout_testing', - 'user' => getenv('DB_USER') ?? 'db', - 'password' => getenv('DB_PASSWORD') ?? 'db', + 'user' => getenv('DB_USER') ?? 'root', + 'password' => getenv('DB_PASSWORD') ?? 'root', 'schema' => getenv('DB_SCHEMA'), 'tablePrefix' => '', 'driver' => 'mysql', From 4d5daa9e350bfcaa78c372f3f61699ec0e2b6968 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 15 Nov 2023 19:44:47 +0100 Subject: [PATCH 4/9] More tests --- .github/workflows/tests.yml | 1 + tests/_craft/config/db.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e80efab..f68c51b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -21,6 +21,7 @@ jobs: DB_PASSWORD: root DB_PORT: 3306 DB_DRIVER: mysql + DB_SERVER: 127.0.0.1 APP_ENV: testing steps: diff --git a/tests/_craft/config/db.php b/tests/_craft/config/db.php index 20481e0..cff77ff 100644 --- a/tests/_craft/config/db.php +++ b/tests/_craft/config/db.php @@ -2,9 +2,9 @@ return [ 'server' => getenv('DB_SERVER') ?? '127.0.0.1', - 'database' => getenv('DB_NAME') ?? 'scout_testing', 'user' => getenv('DB_USER') ?? 'root', 'password' => getenv('DB_PASSWORD') ?? 'root', + 'database' => getenv('DB_NAME') ?? 'scout_testing', 'schema' => getenv('DB_SCHEMA'), 'tablePrefix' => '', 'driver' => 'mysql', From 3d1402564229b46f2d29c56706279e07185445c8 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 15 Nov 2023 19:46:36 +0100 Subject: [PATCH 5/9] Fixed env variable --- tests/_craft/config/db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/_craft/config/db.php b/tests/_craft/config/db.php index cff77ff..4078ec3 100644 --- a/tests/_craft/config/db.php +++ b/tests/_craft/config/db.php @@ -4,7 +4,7 @@ 'server' => getenv('DB_SERVER') ?? '127.0.0.1', 'user' => getenv('DB_USER') ?? 'root', 'password' => getenv('DB_PASSWORD') ?? 'root', - 'database' => getenv('DB_NAME') ?? 'scout_testing', + 'database' => getenv('DB_DATABASE') ?? 'scout_testing', 'schema' => getenv('DB_SCHEMA'), 'tablePrefix' => '', 'driver' => 'mysql', From ea3b937aabd64420325a2c759d50f62bcd6070fc Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 15 Nov 2023 19:47:23 +0100 Subject: [PATCH 6/9] Added 8.2 to test matrix --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f68c51b..9359cc9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -8,7 +8,7 @@ jobs: strategy: fail-fast: true matrix: - php: [8.1, 8.0] + php: [8.2, 8.1, 8.0] os: [ ubuntu-latest ] stability: [stable, dev] dependency-version: [prefer-stable] From 85f8015756c5993226beb77cd9616bc41949731c Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 15 Nov 2023 20:03:39 +0100 Subject: [PATCH 7/9] Check if we have a site property before getting its ID #285 --- src/Scout.php | 2 +- src/jobs/IndexElement.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Scout.php b/src/Scout.php index e6efe18..565b96e 100644 --- a/src/Scout.php +++ b/src/Scout.php @@ -168,7 +168,7 @@ function (ElementEvent $event) { ->push( new IndexElement([ 'id' => $element->id, - 'siteId' => $element->site->id + 'siteId' => $element->site ? $element->site->id : null ]) ); } else { diff --git a/src/jobs/IndexElement.php b/src/jobs/IndexElement.php index 46c49a7..f909fcc 100644 --- a/src/jobs/IndexElement.php +++ b/src/jobs/IndexElement.php @@ -13,8 +13,8 @@ class IndexElement extends BaseJob /** @var int */ public int $id; - /** @var int */ - public int $siteId; + /** @var int|null */ + public int|null $siteId; public function execute($queue): void { From b43ead07245d2099bd6eba7a82c1cd8b9c642d16 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 15 Nov 2023 20:03:55 +0100 Subject: [PATCH 8/9] Remove 8.2 --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9359cc9..f68c51b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -8,7 +8,7 @@ jobs: strategy: fail-fast: true matrix: - php: [8.2, 8.1, 8.0] + php: [8.1, 8.0] os: [ ubuntu-latest ] stability: [stable, dev] dependency-version: [prefer-stable] From c3b3520acb6c1c5982da101bd4b6ee0768795f4a Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Wed, 15 Nov 2023 20:07:42 +0100 Subject: [PATCH 9/9] Changelog for 3.3.2 --- CHANGELOG.md | 5 +++++ composer.json | 2 +- src/Scout.php | 2 +- src/jobs/DeindexElement.php | 4 ++-- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f07b44..62bfe99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to this project will be documented in this file. +## 3.3.2 - 2023-11-15 +### Fixed +- Fixed an error on the Control Panel Utility page ([#284](https://github.com/studioespresso/craft-scout/issues/284)) +- Fixed an error when saving elements without a site property ([#285](https://github.com/studioespresso/craft-scout/issues/285****)) + ## 3.3.1 - 2023-11-08 ### Added diff --git a/composer.json b/composer.json index 5aeb77a..1b82bbb 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "studioespresso/craft-scout", "description": "Craft Scout provides a simple solution for adding full-text search to your entries. Scout will automatically keep your search indexes in sync with your entries.", "type": "craft-plugin", - "version": "3.3.1", + "version": "3.3.2", "keywords": [ "craft", "cms", diff --git a/src/Scout.php b/src/Scout.php index 565b96e..9130fdf 100644 --- a/src/Scout.php +++ b/src/Scout.php @@ -203,7 +203,7 @@ function (ElementEvent $event) { ->push( new DeindexElement([ 'id' => $element->id, - 'siteId' => $element->site->id + 'siteId' => $element->site ? $element->site->id : null ]) ); } diff --git a/src/jobs/DeindexElement.php b/src/jobs/DeindexElement.php index cff5e6b..50149e9 100644 --- a/src/jobs/DeindexElement.php +++ b/src/jobs/DeindexElement.php @@ -11,8 +11,8 @@ class DeindexElement extends BaseJob /** @var int */ public $id; - /** @var int */ - public int $siteId; + /** @var int|null */ + public int|null $siteId; public function execute($queue): void {