You are here

public function ContentEntityMetatagTest::testMetatagsField in Translation Management Tool 8

Tests the metatag integration.

File

sources/content/tests/src/Kernel/ContentEntityMetatagTest.php, line 50

Class

ContentEntityMetatagTest
Content entity Source unit tests.

Namespace

Drupal\Tests\tmgmt_content\Kernel

Code

public function testMetatagsField() {

  // Create an english test entity.
  $values = [
    'langcode' => 'en',
    'user_id' => 1,
    'name' => 'Test entity',
    'field_meta_tags' => [
      'value' => serialize([
        'description' => 'The description',
        'robots' => 'noindex,nofollow',
        'referer' => 'origin',
        'news_keywords' => 'Sport',
      ]),
    ],
  ];
  $entity_test = EntityTestMul::create($values);
  $entity_test
    ->save();
  $job = tmgmt_job_create('en', 'de');
  $job->translator = 'test_translator';
  $job
    ->save();
  $job_item = tmgmt_job_item_create('content', $this->entityTypeId, $entity_test
    ->id(), array(
    'tjid' => $job
      ->id(),
  ));
  $job_item
    ->save();
  $source_plugin = $this->container
    ->get('plugin.manager.tmgmt.source')
    ->createInstance('content');
  $data = $source_plugin
    ->getData($job_item);

  // Test the expected structure of the metatags field.
  $expected_field_data = [
    'basic' => [
      '#label' => 'Basic tags',
      'description' => [
        '#translate' => TRUE,
        '#text' => 'The description',
        '#label' => 'Description',
      ],
    ],
    'advanced' => [
      '#label' => 'Advanced',
      'robots' => [
        '#translate' => FALSE,
        '#text' => 'noindex,nofollow',
        '#label' => 'Robots',
      ],
      'news_keywords' => [
        '#translate' => TRUE,
        '#text' => 'Sport',
        '#label' => 'News Keywords',
      ],
    ],
    '#label' => 'Meta tags',
  ];
  $this
    ->assertEquals($expected_field_data, $data['field_meta_tags']);

  // Now request a translation and save it back.
  $job
    ->requestTranslation();
  $items = $job
    ->getItems();
  $item = reset($items);
  $item
    ->acceptTranslation();
  $data = $item
    ->getData();

  // Check that the translations were saved correctly.
  $entity_test = EntityTestMul::load($entity_test
    ->id());
  $translation = $entity_test
    ->getTranslation('de');
  $this
    ->assertEquals($translation->name->value, $data['name'][0]['value']['#translation']['#text']);
  $translated_meta_tags = unserialize($translation
    ->get('field_meta_tags')->value);
  $expected_meta_tags = [
    'description' => 'de(de-ch): The description',
    'robots' => 'noindex,nofollow',
    'news_keywords' => 'de(de-ch): Sport',
  ];
  $this
    ->assertEquals($expected_meta_tags, $translated_meta_tags);
}