public function SerializationTest::testIndexSerialization in Search API 8
Tests that serialization of index entities doesn't lead to data loss.
File
- tests/
src/ Kernel/ System/ SerializationTest.php, line 66
Class
- SerializationTest
- Tests that various classes can be properly serialized and/or cloned.
Namespace
Drupal\Tests\search_api\Kernel\SystemCode
public function testIndexSerialization() {
$index = $this->index;
// Make some changes to the index to ensure they're saved, too.
$field_helper = \Drupal::getContainer()
->get('search_api.fields_helper');
$field_info = [
'type' => 'date',
'datasource_id' => 'entity:node',
'property_path' => 'uid:entity:created',
];
$index
->addField($field_helper
->createField($index, 'test1', $field_info));
$plugin_creation_helper = \Drupal::getContainer()
->get('search_api.plugin_helper');
$index
->addDatasource($plugin_creation_helper
->createDatasourcePlugin($index, 'entity:user'));
$index
->addProcessor($plugin_creation_helper
->createProcessorPlugin($index, 'highlight'));
$index
->setTracker($plugin_creation_helper
->createTrackerPlugin($index, 'search_api_test'));
/** @var \Drupal\search_api\IndexInterface $serialized */
$serialized = unserialize(serialize($index));
$this
->assertNotEmpty($serialized);
$storage = \Drupal::entityTypeManager()
->getStorage('search_api_index');
$index
->preSave($storage);
$serialized
->preSave($storage);
$this
->assertEquals($index
->toArray(), $serialized
->toArray());
// Make sure no object properties will be serialized for an index.
$index
->getDatasources();
$index
->getFields();
$index
->getProcessors();
$index
->getTrackerInstance();
$index
->getPropertyDefinitions(NULL);
$contains_object = function ($var) use (&$contains_object) {
if (is_object($var)) {
return TRUE;
}
if (is_array($var)) {
foreach ($var as $value) {
if ($contains_object($value)) {
return TRUE;
}
}
}
return FALSE;
};
$to_serialize = $index
->__sleep();
foreach ($to_serialize as $property) {
$this
->assertFalse($contains_object($index
->get($property)), "Serialized property \${$property} contains an object.");
}
}