You are here

public function ContentHasherTest::testContentHasher in Tome 8

@covers \Drupal\tome_sync\ContentHasher::writeHash @covers \Drupal\tome_sync\EventSubscriber\ContentHasherEventSubscriber::writeHash

File

modules/tome_sync/tests/src/Kernel/ContentHasherTest.php, line 36

Class

ContentHasherTest
Tests that the content hasher works.

Namespace

Drupal\Tests\tome_sync\Kernel

Code

public function testContentHasher() {

  /** @var \Drupal\tome_sync\ContentHasherInterface $content_hasher */
  $content_hasher = \Drupal::service('tome_sync.content_hasher');
  $uuid = \Drupal::service('uuid')
    ->generate();
  $article = Node::create([
    'type' => 'article',
    'title' => 'My article',
    'uuid' => $uuid,
  ]);
  $article
    ->save();
  $content_name = TomeSyncHelper::getContentName($article);

  // If the hash in the database is different, the content is changed.
  $this
    ->assertTrue(empty($content_hasher
    ->getChangelist()['modified']));
  $content_hasher
    ->writeHash('foo', $content_name);
  $this
    ->assertFalse(empty($content_hasher
    ->getChangelist()['modified']));

  // If the hash in the database is missing, the content is new.
  $this
    ->assertTrue(empty($content_hasher
    ->getChangelist()['added']));
  \Drupal::database()
    ->truncate('tome_sync_content_hash')
    ->execute();
  $this
    ->assertFalse(empty($content_hasher
    ->getChangelist()['added']));
  $this
    ->assertTrue(empty($content_hasher
    ->getChangelist()['modified']));

  // If the hash is missing completely, nothing happens.
  $article
    ->delete();
  $this
    ->assertTrue(empty($content_hasher
    ->getChangelist()['added']));
  $this
    ->assertTrue(empty($content_hasher
    ->getChangelist()['modified']));

  // If the hash is only in the database, the content is deleted.
  $this
    ->assertTrue(empty($content_hasher
    ->getChangelist()['deleted']));
  $content_hasher
    ->writeHash('foo', $content_name);
  $this
    ->assertFalse(empty($content_hasher
    ->getChangelist()['deleted']));

  // If the hashes are the same, nothing happens.
  $content_hasher
    ->deleteHash($content_name);
  $this
    ->assertTrue(empty($content_hasher
    ->getChangelist()['deleted']));
  $this
    ->assertTrue(empty($content_hasher
    ->getChangelist()['added']));
  $this
    ->assertTrue(empty($content_hasher
    ->getChangelist()['modified']));
}