diff --git a/libraries/src/HTML/Helpers/Content.php b/libraries/src/HTML/Helpers/Content.php index 48bdd19991263..a3200beb6f1c9 100644 --- a/libraries/src/HTML/Helpers/Content.php +++ b/libraries/src/HTML/Helpers/Content.php @@ -83,6 +83,7 @@ public static function months($state) $model->setState($key, $value); } + $model->setState('params', $state->get('params')); $model->setState('filter.category_id', $state->get('category.id')); $model->setState('list.start', 0); $model->setState('list.limit', -1); diff --git a/tests/Unit/Libraries/Cms/Html/ContentTest.php b/tests/Unit/Libraries/Cms/Html/ContentTest.php new file mode 100644 index 0000000000000..2cd36119cc9e7 --- /dev/null +++ b/tests/Unit/Libraries/Cms/Html/ContentTest.php @@ -0,0 +1,98 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace Joomla\Tests\Unit\Libraries\Cms\Html; + +use Joomla\CMS\Application\CMSApplicationInterface; +use Joomla\CMS\Factory; +use Joomla\CMS\HTML\Helpers\Content; +use Joomla\CMS\MVC\Factory\MVCFactoryInterface; +use Joomla\Component\Content\Administrator\Extension\ContentComponent; +use Joomla\Component\Content\Site\Model\ArticlesModel; +use Joomla\Registry\Registry; +use Joomla\Tests\Unit\UnitTestCase; + +/** + * Test class for Content helper. + * + * @package Joomla.UnitTest + * @subpackage Html + * @since __DEPLOY_VERSION__ + */ +class ContentTest extends UnitTestCase +{ + /** + * @var CMSApplicationInterface|null + */ + private $application; + + /** + * @return void + * + * @since __DEPLOY_VERSION__ + */ + protected function setUp(): void + { + parent::setUp(); + + $this->application = Factory::$application; + } + + /** + * @return void + * + * @since __DEPLOY_VERSION__ + */ + protected function tearDown(): void + { + Factory::$application = $this->application; + + parent::tearDown(); + } + + /** + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function testMonthsPreservesParamsRegistry(): void + { + $params = new Registry(['orderby_sec' => 'created']); + $state = new Registry([ + 'category.id' => 7, + 'filter.published' => 1, + ]); + $state->set('params', $params); + $modelState = []; + $model = $this->createMock(ArticlesModel::class); + $model->method('setState')->willReturnCallback(function ($key, $value) use (&$modelState) { + $modelState[$key] = $value; + }); + $model->method('countItemsByMonth')->willReturnCallback(function () use (&$modelState) { + $modelState['params']->get('orderby_sec'); + + return []; + }); + + $mvcFactory = $this->createStub(MVCFactoryInterface::class); + $mvcFactory->method('createModel')->willReturn($model); + + $component = $this->createStub(ContentComponent::class); + $component->method('getMVCFactory')->willReturn($mvcFactory); + + $application = $this->createStub(CMSApplicationInterface::class); + $application->method('bootComponent')->willReturn($component); + Factory::$application = $application; + + $this->assertSame([], Content::months($state)); + $this->assertSame($params, $modelState['params']); + $this->assertSame(1, $modelState['filter.published']); + } +}