From 6082760f85b26eed43d580a831361ec05f91b03f Mon Sep 17 00:00:00 2001 From: j3nsch Date: Sat, 4 Apr 2020 19:26:57 +0200 Subject: [PATCH] OPUSVIER-1907 Added option of filtering by modules. --- library/Opus/Translate/Dao.php | 10 +++++++- tests/Opus/Translate/DaoTest.php | 41 +++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/library/Opus/Translate/Dao.php b/library/Opus/Translate/Dao.php index f585f2a4e..6082c461f 100644 --- a/library/Opus/Translate/Dao.php +++ b/library/Opus/Translate/Dao.php @@ -312,7 +312,7 @@ public function renameKey($key, $newKey, $module = 'default') $database->commit(); } - public function getTranslationsWithModules() + public function getTranslationsWithModules($modules = null) { $table = Opus_Db_TableGateway::getInstance('Opus_Db_Translations'); @@ -320,6 +320,14 @@ public function getTranslationsWithModules() ->from(['t' => 'translations'], ['keys.key', 'locale', 'value', 'keys.module']) ->join(['keys' => 'translationkeys'], 't.key_id = keys.id'); + if (! is_null($modules)) { + if (is_array($modules)) { + $select->where('keys.module IN (?)', $modules); + } else { + $select->where('keys.module = ?', $modules); + } + } + $rows = $table->getAdapter()->fetchAll($select); $result = []; diff --git a/tests/Opus/Translate/DaoTest.php b/tests/Opus/Translate/DaoTest.php index 50428fb8e..c7fcb1dd3 100644 --- a/tests/Opus/Translate/DaoTest.php +++ b/tests/Opus/Translate/DaoTest.php @@ -520,7 +520,7 @@ public function testSetSpecialTranslation() $translations = $dao->getTranslation('admin-actionbox-goto-section'); - var_dump($translations); + $this->assertEquals($data, $translations); } public function testGetTranslationsWithModules() @@ -547,6 +547,45 @@ public function testGetTranslationsWithModules() $this->assertEquals($data, $testKey['values']); } + public function testGetTranslationsWithModulesFilteredByModules() + { + $dao = new Opus_Translate_Dao(); + + $keyData = ['en' => 'keyEN', 'de' => 'keyDE']; + $defaultKeyData = ['en' => 'defaultKeyEN', 'de' => 'defaultKeyDE']; + $publishKeyData = ['en' => 'publishKeyEN', 'de' => 'publishKeyDE']; + $adminKeyData = ['en' => 'adminKeyEN', 'de' => 'adminKeyDE']; + + $dao->setTranslation('key', $keyData); + $dao->setTranslation('default_key', $defaultKeyData, 'default'); + $dao->setTranslation('publish_key', $publishKeyData, 'publish'); + $dao->setTranslation('admin_key', $adminKeyData, 'admin'); + + // test no module specified + $translations = $dao->getTranslationsWithModules(); + + $this->assertCount(4, $translations); + + // test single module + $translations = $dao->getTranslationsWithModules('default'); + + $this->assertCount(2, $translations); + $this->assertArrayHasKey('key', $translations); + $this->assertArrayHasKey('default_key', $translations); + + // test multiple modules + $translations = $dao->getTranslationsWithModules(['publish', 'admin']); + + $this->assertCount(2, $translations); + $this->assertArrayHasKey('publish_key', $translations); + $this->assertArrayHasKey('admin_key', $translations); + + // test unknown module + $translations = $dao->getTranslationsWithModules('unknown857'); + + $this->assertCount(0, $translations); + } + public function testGetModules() { $dao = new Opus_Translate_Dao();