Skip to content

Commit ed24322

Browse files
laoneobrianteemanroland-d
authored
[4.2] Load the actionlog params from model instead of the helper (#37592)
* Load the actionlog params from model instead of the helper * Typehints and null test * Use common function with stability checks * Update plugins/actionlog/joomla/joomla.php Co-authored-by: Brian Teeman <[email protected]> * Update plugins/actionlog/joomla/joomla.php Co-authored-by: Brian Teeman <[email protected]> * use samce code as branch * import Co-authored-by: Brian Teeman <[email protected]> Co-authored-by: Roland Dalmulder <[email protected]>
1 parent cfe2869 commit ed24322

File tree

7 files changed

+167
-38
lines changed

7 files changed

+167
-38
lines changed

administrator/components/com_actionlogs/src/Helper/ActionlogsHelper.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,13 @@ public static function loadTranslationFiles($extension)
158158
* @return mixed An object contains content type parameters, or null if not found
159159
*
160160
* @since 3.9.0
161+
*
162+
* @deprecated 5.0 Use the action log config model instead
161163
*/
162164
public static function getLogContentTypeParams($context)
163165
{
164-
$db = Factory::getDbo();
165-
$query = $db->getQuery(true)
166-
->select('a.*')
167-
->from($db->quoteName('#__action_log_config', 'a'))
168-
->where($db->quoteName('a.type_alias') . ' = :context')
169-
->bind(':context', $context);
170-
171-
$db->setQuery($query);
172-
173-
return $db->loadObject();
166+
return Factory::getApplication()->bootComponent('actionlogs')->getMVCFactory()
167+
->createModel('ActionlogConfig', 'Administrator')->getLogContentTypeParams($context);
174168
}
175169

176170
/**
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* @package Joomla.Administrator
4+
* @subpackage com_actionlogs
5+
*
6+
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
7+
* @license GNU General Public License version 2 or later; see LICENSE.txt
8+
*/
9+
10+
namespace Joomla\Component\Actionlogs\Administrator\Model;
11+
12+
\defined('_JEXEC') or die;
13+
14+
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
15+
use stdClass;
16+
17+
/**
18+
* Model to interact with the action log configuration.
19+
*
20+
* @since __DEPLOY_VERSION__
21+
*/
22+
class ActionlogConfigModel extends BaseDatabaseModel
23+
{
24+
/**
25+
* Returns the action logs config for the given context.
26+
*
27+
* @param string $context The context of the content
28+
*
29+
* @return stdClass|null An object contains content type parameters, or null if not found
30+
*
31+
* @since __DEPLOY_VERSION__
32+
*/
33+
public function getLogContentTypeParams(string $context): ?stdClass
34+
{
35+
$db = $this->getDatabase();
36+
$query = $db->getQuery(true)
37+
->select('a.*')
38+
->from($db->quoteName('#__action_log_config', 'a'))
39+
->where($db->quoteName('a.type_alias') . ' = :context')
40+
->bind(':context', $context);
41+
42+
$db->setQuery($query);
43+
44+
return $db->loadObject();
45+
}
46+
}

plugins/actionlog/joomla/joomla.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Joomla\CMS\Component\ComponentHelper;
1313
use Joomla\CMS\Factory;
1414
use Joomla\CMS\Installer\Installer;
15+
use Joomla\CMS\MVC\Factory\MVCFactoryServiceInterface;
1516
use Joomla\CMS\Table\Table;
1617
use Joomla\CMS\User\User;
1718
use Joomla\Component\Actionlogs\Administrator\Helper\ActionlogsHelper;
@@ -98,7 +99,7 @@ public function onContentAfterSave($context, $article, $isNew): void
9899
$context = $this->contextAliases[$context];
99100
}
100101

101-
$params = ActionlogsHelper::getLogContentTypeParams($context);
102+
$params = $this->getActionLogParams($context);
102103

103104
// Not found a valid content type, don't process further
104105
if ($params === null)
@@ -164,7 +165,7 @@ public function onContentAfterDelete($context, $article): void
164165
return;
165166
}
166167

167-
$params = ActionlogsHelper::getLogContentTypeParams($context);
168+
$params = $this->getActionLogParams($context);
168169

169170
// Not found a valid content type, don't process further
170171
if ($params === null)
@@ -216,7 +217,7 @@ public function onContentChangeState($context, $pks, $value)
216217
return;
217218
}
218219

219-
$params = ActionlogsHelper::getLogContentTypeParams($context);
220+
$params = $this->getActionLogParams($context);
220221

221222
// Not found a valid content type, don't process further
222223
if ($params === null)
@@ -513,7 +514,7 @@ public function onExtensionAfterSave($context, $table, $isNew): void
513514
return;
514515
}
515516

516-
$params = ActionlogsHelper::getLogContentTypeParams($context);
517+
$params = $this->getActionLogParams($context);
517518

518519
// Not found a valid content type, don't process further
519520
if ($params === null)
@@ -570,7 +571,7 @@ public function onExtensionAfterDelete($context, $table): void
570571
return;
571572
}
572573

573-
$params = ActionlogsHelper::getLogContentTypeParams($context);
574+
$params = $this->getActionLogParams($context);
574575

575576
// Not found a valid content type, don't process further
576577
if ($params === null)
@@ -1170,4 +1171,25 @@ public function onJoomlaAfterUpdate($oldVersion = null)
11701171
);
11711172
$this->addLog(array($message), 'PLG_ACTIONLOG_JOOMLA_USER_UPDATE', $context, $user->id);
11721173
}
1174+
1175+
/**
1176+
* Returns the action log params for the given context.
1177+
*
1178+
* @param string $context The context of the action log
1179+
*
1180+
* @return stdClass The params
1181+
*
1182+
* @since __DEPLOY_VERSION__
1183+
*/
1184+
private function getActionLogParams($context): ?stdClass
1185+
{
1186+
$component = $this->app->bootComponent('actionlogs');
1187+
1188+
if (!$component instanceof MVCFactoryServiceInterface)
1189+
{
1190+
return null;
1191+
}
1192+
1193+
return $component->getMVCFactory()->createModel('ActionlogConfig', 'Administrator')->getLogContentTypeParams($context);
1194+
}
11731195
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* @package Joomla.UnitTest
4+
* @subpackage Extension
5+
*
6+
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
7+
* @license GNU General Public License version 2 or later; see LICENSE.txt
8+
*/
9+
10+
namespace Joomla\Tests\Unit\Components\Actionlogs;
11+
12+
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
13+
use Joomla\Component\Actionlogs\Administrator\Model\ActionlogConfigModel;
14+
use Joomla\Database\DatabaseInterface;
15+
use Joomla\Tests\Unit\UnitTestCase;
16+
17+
/**
18+
* Test class for ActionlogConfigModel
19+
*
20+
* @package Joomla.UnitTest
21+
* @subpackage Actionlog
22+
* @since __DEPLOY_VERSION__
23+
*/
24+
class ActionlogConfigModelTest extends UnitTestCase
25+
{
26+
/**
27+
* @testdox Test that getLogContentTypeParams returns the correct params
28+
*
29+
* @return void
30+
*
31+
* @since __DEPLOY_VERSION__
32+
*/
33+
public function testGetLogContentTypeParams()
34+
{
35+
$config = new \stdClass;
36+
$db = $this->createStub(DatabaseInterface::class);
37+
$db->method('getQuery')->willReturn($this->getQueryStub($db));
38+
$db->method('loadObject')->willReturn($config);
39+
40+
$model = new ActionlogConfigModel(['dbo' => $db], $this->createStub(MVCFactoryInterface::class));
41+
42+
$this->assertEquals($config, $model->getLogContentTypeParams('test'));
43+
}
44+
45+
/**
46+
* @testdox Test that getLogContentTypeParams returns null when not found
47+
*
48+
* @return void
49+
*
50+
* @since __DEPLOY_VERSION__
51+
*/
52+
public function testGetNullLogContentTypeParams()
53+
{
54+
$db = $this->createStub(DatabaseInterface::class);
55+
$db->method('getQuery')->willReturn($this->getQueryStub($db));
56+
57+
$model = new ActionlogConfigModel(['dbo' => $db], $this->createStub(MVCFactoryInterface::class));
58+
59+
$this->assertNull($model->getLogContentTypeParams('test'));
60+
}
61+
}

tests/Unit/Libraries/Cms/MVC/Model/DatabaseModelTest.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use Joomla\CMS\MVC\Model\DatabaseAwareTrait;
1616
use Joomla\CMS\Table\Table;
1717
use Joomla\Database\DatabaseInterface;
18-
use Joomla\Database\DatabaseQuery;
19-
use Joomla\Database\QueryInterface;
2018
use Joomla\Tests\Unit\UnitTestCase;
2119

2220
/**
@@ -261,25 +259,4 @@ public function cache($key, $value)
261259

262260
$this->assertEquals('test', $model->cache(1, 'test'));
263261
}
264-
265-
/**
266-
* Returns a database query instance.
267-
*
268-
* @param DatabaseInterface $db The database
269-
*
270-
* @return QueryInterface
271-
*
272-
* @since 4.2.0
273-
*/
274-
private function getQueryStub(DatabaseInterface $db): QueryInterface
275-
{
276-
return new class($db) extends DatabaseQuery
277-
{
278-
public function groupConcat($expression, $separator = ',')
279-
{}
280-
281-
public function processLimit($query, $limit, $offset = 0)
282-
{}
283-
};
284-
}
285262
}

tests/Unit/UnitTestCase.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,35 @@
88
*/
99
namespace Joomla\Tests\Unit;
1010

11+
use Joomla\Database\DatabaseInterface;
12+
use Joomla\Database\DatabaseQuery;
13+
use Joomla\Database\QueryInterface;
14+
1115
/**
1216
* Base Unit Test case for common behaviour across unit tests
1317
*
1418
* @since 4.0.0
1519
*/
1620
abstract class UnitTestCase extends \PHPUnit\Framework\TestCase
1721
{
22+
/**
23+
* Returns a database query instance.
24+
*
25+
* @param DatabaseInterface $db The database
26+
*
27+
* @return QueryInterface
28+
*
29+
* @since __DEPLOY_VERSION__
30+
*/
31+
protected function getQueryStub(DatabaseInterface $db): QueryInterface
32+
{
33+
return new class($db) extends DatabaseQuery
34+
{
35+
public function groupConcat($expression, $separator = ',')
36+
{}
37+
38+
public function processLimit($query, $limit, $offset = 0)
39+
{}
40+
};
41+
}
1842
}

tests/Unit/bootstrap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,10 @@ class_exists('\\Joomla\\CMS\\Autoload\\ClassLoader');
125125
// Register the class aliases for Framework classes that have replaced their Platform equivalents
126126
require_once JPATH_LIBRARIES . '/classmap.php';
127127

128+
// Load extension classes
129+
require_once JPATH_LIBRARIES . '/namespacemap.php';
130+
$extensionPsr4Loader = new \JNamespacePsr4Map;
131+
$extensionPsr4Loader->load();
132+
128133
// Define the Joomla version if not already defined.
129134
defined('JVERSION') or define('JVERSION', (new \Joomla\CMS\Version)->getShortVersion());

0 commit comments

Comments
 (0)