Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_actionlogs
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @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();
}
}
32 changes: 27 additions & 5 deletions plugins/actionlog/joomla/joomla.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
62 changes: 62 additions & 0 deletions tests/Unit/Components/Actionlogs/ActionlogConfigModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* @package Joomla.UnitTest
* @subpackage Extension
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @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;
use stdClass;

/**
* 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'));
}
}
23 changes: 0 additions & 23 deletions tests/Unit/Libraries/Cms/MVC/Model/DatabaseModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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)
{}
};
}
}
24 changes: 24 additions & 0 deletions tests/Unit/UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,35 @@
*/
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
*
* @since 4.0.0
*/
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)
{}
};
}
}
5 changes: 5 additions & 0 deletions tests/Unit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());