Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
93 changes: 59 additions & 34 deletions administrator/components/com_finder/src/Indexer/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,35 @@ public function setLanguage()
*/
public function serialize()
{
$taxonomy = array();
return serialize($this->__serialize());
}

/**
* Helper function to unserialise the data for this object
*
* @param string $serialized Serialised data to unserialise
*
* @return void
*
* @since 4.0.0
*/
public function unserialize($serialized): void
{
$this->__unserialize(unserialize($serialized));
}

/**
* Magic method used for serializing.
*
* @since __DEPLOY_VERSION__
*/
public function __serialize(): array
{
$taxonomy = [];

foreach ($this->taxonomy as $branch => $nodes)
{
$taxonomy[$branch] = array();
$taxonomy[$branch] = [];

foreach ($nodes as $node)
{
Expand All @@ -487,8 +511,8 @@ public function serialize()
}
}

return serialize(
[
// IMPORTANT - This order must match EXACTLY the order of the $properties in the self::__unserialize method
return [
$this->access,
$this->defaultLanguage,
$this->description,
Expand All @@ -508,42 +532,43 @@ public function serialize()
$this->title,
$this->type_id,
$this->url
]
);
];
}

/**
* Helper function to unserialise the data for this object
*
* @param string $serialized Serialised data to unserialise
*
* @return void
* Magic method used for unserializing.
*
* @since 4.0.0
* @since __DEPLOY_VERSION__
*/
public function unserialize($serialized)
public function __unserialize(array $serialized): void
{
list(
$this->access,
$this->defaultLanguage,
$this->description,
$this->elements,
$this->end_date,
$this->instructions,
$this->language,
$this->list_price,
$this->publish_end_date,
$this->publish_start_date,
$this->published,
$this->route,
$this->sale_price,
$this->start_date,
$this->state,
$this->taxonomy,
$this->title,
$this->type_id,
$this->url
) = unserialize($serialized);
// IMPORTANT - This order must match EXACTLY the order of the array in the self::__serialize method
$properties = [
'access',
'defaultLanguage',
'description',
'elements',
'end_date',
'instructions',
'language',
'list_price',
'publish_end_date',
'publish_start_date',
'published',
'route',
'sale_price',
'start_date',
'state',
'taxonomy',
'title',
'type_id',
'url',
];

foreach ($properties as $k => $v)
{
$this->$v = $serialized[$k];
}

foreach ($this->taxonomy as $nodes)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* @package Joomla.UnitTest
* @subpackage com_finder
*
* @copyright (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Tests\Unit\Administrator\Components\Finder\Indexer;

use Joomla\Component\Finder\Administrator\Indexer\Result;
use Joomla\Tests\Unit\UnitTestCase;
use ReflectionClass;

/**
* Test class for \Joomla\Component\Finder\Administrator\Indexer\Result
*
* @since __DEPLOY_VERSION__
*/
class ResultTest extends UnitTestCase
{
/**
* Include non-autoloaded files as Namespace in the files that don't implement PSR-4
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
protected function setUp(): void
{
// Can be removed once we have autoloading working in Unit Tests
// @see https://github.com/joomla/joomla-cms/pull/36486
if (!class_exists(Result::class))
{
require_once JPATH_ADMINISTRATOR . '/components/com_finder/src/Indexer/Indexer.php';
require_once JPATH_ADMINISTRATOR . '/components/com_finder/src/Indexer/Result.php';
}
}

/**
* @return void
*
* @throws \ReflectionException
*
* @covers Result::unserialize
* @covers Result::serialize
* @covers Result::__serialize
* @covers Result::__unserialize
*
* @since __DEPLOY_VERSION__
*/
public function testSerialize(): void
{
/** @var Result $obj */
$obj = $this->createNoConstructorMock();
$obj->setElement('this', 'that');
$obj->title = 'MyTitle';

// Test the `serialize` method provided by the object - Pre PHP 8.1 deprecated style
$this->assertIsString(
$obj->serialize($obj)
);

// Test PHP `serialize` the object - PHP 8.1+ style (uses magic methods)
$this->assertEquals(
'that',
unserialize(serialize($obj))->getElement('this')
);

$obj->title = 'MyTitle2';
$this->assertEquals(
'MyTitle2',
unserialize(serialize($obj))->title
);
}

/**
* Useful method to mock the Result class so that the constructor doesn't call
* ComponentHelper which requires a db connection and full Joomla stack running.
*
* @param $class string
*
* @return object<Result>
*
* @throws \ReflectionException
*
* @since __DEPLOY_VERSION__
*/
private function createNoConstructorMock($class = Result::class): object
{
return (new ReflectionClass($class))->newInstanceWithoutConstructor();
}
}