You are here

public function ExporterTest::testExportContent in Tome 8

@covers \Drupal\tome_sync\Exporter::exportContent

File

modules/tome_sync/tests/src/Kernel/ExporterTest.php, line 38

Class

ExporterTest
Tests that the exporter works.

Namespace

Drupal\Tests\tome_sync\Kernel

Code

public function testExportContent() {

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');

  /** @var \Drupal\Core\Config\StorageInterface $storage */
  $storage = \Drupal::service('tome_sync.storage.content');
  $user = $this
    ->createUser();
  $article = Node::create([
    'type' => 'article',
    'title' => 'My article',
    'uid' => $user
      ->id(),
  ]);
  $article
    ->save();
  $page = Node::create([
    'type' => 'page',
    'title' => 'My page',
    'field_reference' => [
      'target_id' => $article
        ->id(),
    ],
    'uid' => $user
      ->id(),
  ]);
  $page
    ->save();
  $data = $storage
    ->read(TomeSyncHelper::getContentName($page));
  $exported_page = $serializer
    ->denormalize($data, Node::class, 'json');
  $this
    ->assertEquals($page
    ->uuid(), $exported_page
    ->uuid());
  $this
    ->assertEquals($page
    ->getTitle(), $exported_page
    ->getTitle());
  $this
    ->assertEquals($article
    ->id(), $exported_page->field_reference->target_id);
  $page
    ->addTranslation('fr', [
    'title' => 'My french page',
  ])
    ->save();
  $data = $storage
    ->read(TomeSyncHelper::getContentName($page
    ->getTranslation('fr')));
  $exported_page = $serializer
    ->denormalize($data, Node::class, 'json');
  $this
    ->assertEquals('My french page', $exported_page
    ->getTitle());
  $index_file = Settings::get('tome_content_directory', '../content') . '/meta/index.json';
  $this
    ->assertFileExists($index_file);
  $index = json_decode(file_get_contents($index_file), TRUE);
  $user_name = TomeSyncHelper::getContentName($user);
  $page_name = TomeSyncHelper::getContentName($page);
  $article_name = TomeSyncHelper::getContentName($article);
  $this
    ->assertArrayHasKey($user_name, $index);
  $this
    ->assertArrayHasKey($page_name, $index);
  $this
    ->assertArrayHasKey($article_name, $index);
  $this
    ->assertEmpty($index[$user_name]);
  $this
    ->assertContains($article_name, $index[$page_name]);
  $this
    ->assertContains($user_name, $index[$page_name]);
  $this
    ->assertContains($user_name, $index[$article_name]);
}