MetatagFieldSerializerTest.php in Acquia Content Hub 8.2
File
modules/acquia_contenthub_metatag/tests/src/Kernel/EventSubscriber/SerializeContentField/MetatagFieldSerializerTest.php
View source
<?php
namespace Drupal\Tests\acquia_contenthub_metatag\Kernel\EventSubscriber\SerializeContentField;
use Drupal\Tests\acquia_contenthub\Kernel\AcquiaContentHubSerializerTestBase;
class MetatagFieldSerializerTest extends AcquiaContentHubSerializerTestBase {
protected const TABLE_NAME = 'acquia_contenthub_publisher_export_tracking';
protected const FIELD_NAME = 'field_meta_tags';
public static $modules = [
'acquia_contenthub_metatag',
'metatag',
'token',
];
public function setUp() : void {
parent::setUp();
self::$modules = array_merge(parent::$modules, self::$modules);
$this
->createContentType('field_meta_tags', 'metatag');
}
public function testTransformMetatagValues(int $do_not_transform, string $rand_str) {
$values = [
self::FIELD_NAME => serialize([
'title' => '[node:title] | [site:name]',
'description' => '[node:summary]',
'canonical_url' => '[node:url]' . $rand_str,
]),
];
$this->entity = $this
->createNode($values);
$field = $this->entity
->get(self::FIELD_NAME);
$expected_output = $this
->prepareOutput($do_not_transform, $rand_str);
$event = $this
->dispatchSerializeEvent(self::FIELD_NAME, $field);
$langcode = $event
->getEntity()
->language()
->getId();
$actual_output = unserialize($event
->getFieldData()['value'][$langcode]['value']);
$this
->assertEquals($expected_output, $actual_output);
}
public function dataProvider() : array {
$random_string = $this
->randomMachineName();
return [
[
0,
$random_string,
],
[
1,
$random_string,
],
];
}
protected function prepareOutput(int $do_not_transform, string $rand_str) : array {
$this->configFactory
->getEditable('acquia_contenthub_metatag.settings')
->set('ach_metatag_node_url_do_not_transform', $do_not_transform)
->save();
$canonical_url = $do_not_transform ? '[node:url]' . $rand_str : $this->entity
->toUrl()
->setAbsolute()
->toString() . $rand_str;
return [
"title" => "[node:title] | [site:name]",
"description" => "[node:summary]",
"canonical_url" => $canonical_url,
];
}
public function tearDown() : void {
$node_type = $this->entityTypeManager
->getStorage('node_type')
->load(self::BUNDLE);
$node_type
->delete();
$this->configFactory
->getEditable('acquia_contenthub.admin_settings')
->delete();
$this->configFactory
->getEditable('acquia_contenthub_metatag.settings')
->delete();
parent::tearDown();
}
}