diff --git a/administrator/components/com_actionlogs/src/Helper/ActionlogsHelper.php b/administrator/components/com_actionlogs/src/Helper/ActionlogsHelper.php index 6a53818fd175c..309ed9d5dcc11 100644 --- a/administrator/components/com_actionlogs/src/Helper/ActionlogsHelper.php +++ b/administrator/components/com_actionlogs/src/Helper/ActionlogsHelper.php @@ -158,19 +158,13 @@ public static function loadTranslationFiles($extension) * @return mixed An object contains content type parameters, or null if not found * * @since 3.9.0 + * + * @deprecated 5.0 Use the action log config model instead */ public static function getLogContentTypeParams($context) { - $db = Factory::getDbo(); - $query = $db->getQuery(true) - ->select('a.*') - ->from($db->quoteName('#__action_log_config', 'a')) - ->where($db->quoteName('a.type_alias') . ' = :context') - ->bind(':context', $context); - - $db->setQuery($query); - - return $db->loadObject(); + return Factory::getApplication()->bootComponent('actionlogs')->getMVCFactory() + ->createModel('ActionlogConfig', 'Administrator')->getLogContentTypeParams($context); } /** diff --git a/administrator/components/com_actionlogs/src/Model/ActionlogConfigModel.php b/administrator/components/com_actionlogs/src/Model/ActionlogConfigModel.php new file mode 100644 index 0000000000000..9d3829d1e5a82 --- /dev/null +++ b/administrator/components/com_actionlogs/src/Model/ActionlogConfigModel.php @@ -0,0 +1,46 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace Joomla\Component\Actionlogs\Administrator\Model; + +\defined('_JEXEC') or die; + +use Joomla\CMS\MVC\Model\BaseDatabaseModel; +use stdClass; + +/** + * Model to interact with the action log configuration. + * + * @since __DEPLOY_VERSION__ + */ +class ActionlogConfigModel extends BaseDatabaseModel +{ + /** + * Returns the action logs config for the given context. + * + * @param string $context The context of the content + * + * @return stdClass|null An object contains content type parameters, or null if not found + * + * @since __DEPLOY_VERSION__ + */ + public function getLogContentTypeParams(string $context): ?stdClass + { + $db = $this->getDatabase(); + $query = $db->getQuery(true) + ->select('a.*') + ->from($db->quoteName('#__action_log_config', 'a')) + ->where($db->quoteName('a.type_alias') . ' = :context') + ->bind(':context', $context); + + $db->setQuery($query); + + return $db->loadObject(); + } +} diff --git a/plugins/actionlog/joomla/joomla.php b/plugins/actionlog/joomla/joomla.php index 5943241015f4b..1598c820670db 100644 --- a/plugins/actionlog/joomla/joomla.php +++ b/plugins/actionlog/joomla/joomla.php @@ -12,6 +12,7 @@ use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Factory; use Joomla\CMS\Installer\Installer; +use Joomla\CMS\MVC\Factory\MVCFactoryServiceInterface; use Joomla\CMS\Table\Table; use Joomla\CMS\User\User; use Joomla\Component\Actionlogs\Administrator\Helper\ActionlogsHelper; @@ -98,7 +99,7 @@ public function onContentAfterSave($context, $article, $isNew): void $context = $this->contextAliases[$context]; } - $params = ActionlogsHelper::getLogContentTypeParams($context); + $params = $this->getActionLogParams($context); // Not found a valid content type, don't process further if ($params === null) @@ -164,7 +165,7 @@ public function onContentAfterDelete($context, $article): void return; } - $params = ActionlogsHelper::getLogContentTypeParams($context); + $params = $this->getActionLogParams($context); // Not found a valid content type, don't process further if ($params === null) @@ -216,7 +217,7 @@ public function onContentChangeState($context, $pks, $value) return; } - $params = ActionlogsHelper::getLogContentTypeParams($context); + $params = $this->getActionLogParams($context); // Not found a valid content type, don't process further if ($params === null) @@ -513,7 +514,7 @@ public function onExtensionAfterSave($context, $table, $isNew): void return; } - $params = ActionlogsHelper::getLogContentTypeParams($context); + $params = $this->getActionLogParams($context); // Not found a valid content type, don't process further if ($params === null) @@ -570,7 +571,7 @@ public function onExtensionAfterDelete($context, $table): void return; } - $params = ActionlogsHelper::getLogContentTypeParams($context); + $params = $this->getActionLogParams($context); // Not found a valid content type, don't process further if ($params === null) @@ -1170,4 +1171,25 @@ public function onJoomlaAfterUpdate($oldVersion = null) ); $this->addLog(array($message), 'PLG_ACTIONLOG_JOOMLA_USER_UPDATE', $context, $user->id); } + + /** + * Returns the action log params for the given context. + * + * @param string $context The context of the action log + * + * @return stdClass The params + * + * @since __DEPLOY_VERSION__ + */ + private function getActionLogParams($context): ?stdClass + { + $component = $this->app->bootComponent('actionlogs'); + + if (!$component instanceof MVCFactoryServiceInterface) + { + return null; + } + + return $component->getMVCFactory()->createModel('ActionlogConfig', 'Administrator')->getLogContentTypeParams($context); + } } diff --git a/tests/Unit/Components/Actionlogs/ActionlogConfigModelTest.php b/tests/Unit/Components/Actionlogs/ActionlogConfigModelTest.php new file mode 100644 index 0000000000000..639db3266aab7 --- /dev/null +++ b/tests/Unit/Components/Actionlogs/ActionlogConfigModelTest.php @@ -0,0 +1,61 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace Joomla\Tests\Unit\Components\Actionlogs; + +use Joomla\CMS\MVC\Factory\MVCFactoryInterface; +use Joomla\Component\Actionlogs\Administrator\Model\ActionlogConfigModel; +use Joomla\Database\DatabaseInterface; +use Joomla\Tests\Unit\UnitTestCase; + +/** + * Test class for ActionlogConfigModel + * + * @package Joomla.UnitTest + * @subpackage Actionlog + * @since __DEPLOY_VERSION__ + */ +class ActionlogConfigModelTest extends UnitTestCase +{ + /** + * @testdox Test that getLogContentTypeParams returns the correct params + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function testGetLogContentTypeParams() + { + $config = new \stdClass; + $db = $this->createStub(DatabaseInterface::class); + $db->method('getQuery')->willReturn($this->getQueryStub($db)); + $db->method('loadObject')->willReturn($config); + + $model = new ActionlogConfigModel(['dbo' => $db], $this->createStub(MVCFactoryInterface::class)); + + $this->assertEquals($config, $model->getLogContentTypeParams('test')); + } + + /** + * @testdox Test that getLogContentTypeParams returns null when not found + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function testGetNullLogContentTypeParams() + { + $db = $this->createStub(DatabaseInterface::class); + $db->method('getQuery')->willReturn($this->getQueryStub($db)); + + $model = new ActionlogConfigModel(['dbo' => $db], $this->createStub(MVCFactoryInterface::class)); + + $this->assertNull($model->getLogContentTypeParams('test')); + } +} diff --git a/tests/Unit/Libraries/Cms/MVC/Model/DatabaseModelTest.php b/tests/Unit/Libraries/Cms/MVC/Model/DatabaseModelTest.php index 58680b9a5aac1..7c1cb73af1303 100644 --- a/tests/Unit/Libraries/Cms/MVC/Model/DatabaseModelTest.php +++ b/tests/Unit/Libraries/Cms/MVC/Model/DatabaseModelTest.php @@ -15,8 +15,6 @@ use Joomla\CMS\MVC\Model\DatabaseAwareTrait; use Joomla\CMS\Table\Table; use Joomla\Database\DatabaseInterface; -use Joomla\Database\DatabaseQuery; -use Joomla\Database\QueryInterface; use Joomla\Tests\Unit\UnitTestCase; /** @@ -261,25 +259,4 @@ public function cache($key, $value) $this->assertEquals('test', $model->cache(1, 'test')); } - - /** - * Returns a database query instance. - * - * @param DatabaseInterface $db The database - * - * @return QueryInterface - * - * @since 4.2.0 - */ - private function getQueryStub(DatabaseInterface $db): QueryInterface - { - return new class($db) extends DatabaseQuery - { - public function groupConcat($expression, $separator = ',') - {} - - public function processLimit($query, $limit, $offset = 0) - {} - }; - } } diff --git a/tests/Unit/UnitTestCase.php b/tests/Unit/UnitTestCase.php index 5a40be2017dd7..c28306fcc045e 100644 --- a/tests/Unit/UnitTestCase.php +++ b/tests/Unit/UnitTestCase.php @@ -8,6 +8,10 @@ */ namespace Joomla\Tests\Unit; +use Joomla\Database\DatabaseInterface; +use Joomla\Database\DatabaseQuery; +use Joomla\Database\QueryInterface; + /** * Base Unit Test case for common behaviour across unit tests * @@ -15,4 +19,24 @@ */ abstract class UnitTestCase extends \PHPUnit\Framework\TestCase { + /** + * Returns a database query instance. + * + * @param DatabaseInterface $db The database + * + * @return QueryInterface + * + * @since __DEPLOY_VERSION__ + */ + protected function getQueryStub(DatabaseInterface $db): QueryInterface + { + return new class($db) extends DatabaseQuery + { + public function groupConcat($expression, $separator = ',') + {} + + public function processLimit($query, $limit, $offset = 0) + {} + }; + } } diff --git a/tests/Unit/bootstrap.php b/tests/Unit/bootstrap.php index 9e19019197244..383d996c842ec 100644 --- a/tests/Unit/bootstrap.php +++ b/tests/Unit/bootstrap.php @@ -125,5 +125,10 @@ class_exists('\\Joomla\\CMS\\Autoload\\ClassLoader'); // Register the class aliases for Framework classes that have replaced their Platform equivalents require_once JPATH_LIBRARIES . '/classmap.php'; +// Load extension classes +require_once JPATH_LIBRARIES . '/namespacemap.php'; +$extensionPsr4Loader = new \JNamespacePsr4Map; +$extensionPsr4Loader->load(); + // Define the Joomla version if not already defined. defined('JVERSION') or define('JVERSION', (new \Joomla\CMS\Version)->getShortVersion());