You are here

public function UniqueQueueTest::testUpdateHook8001 in Queue Unique 8.2

Test queue_unique_update_8001().

@todo Remove this test method and associated code after the update hook is released so that all md5 references are removed.

File

tests/src/Kernel/UniqueQueueTest.php, line 92

Class

UniqueQueueTest
Unique queue kernel test.

Namespace

Drupal\Tests\queue_unique\Kernel

Code

public function testUpdateHook8001() {
  module_load_install('queue_unique');

  /** @var \Drupal\Core\Database\Schema $database_schema */
  $database_schema = $this->container
    ->get('database')
    ->schema();

  // The table should not exist yet.
  self::assertFalse($database_schema
    ->tableExists(UniqueDatabaseQueue::TABLE_NAME));
  $message = queue_unique_update_8001();
  self::assertStringContainsString('Queue table does not exist', $message);

  // If the table exists and is empty, it should be dropped.
  $old_schema_definition = $this
    ->oldSchemaDefinition();
  $database_schema
    ->createTable(UniqueDatabaseQueue::TABLE_NAME, $old_schema_definition);
  self::assertTrue($database_schema
    ->tableExists(UniqueDatabaseQueue::TABLE_NAME));
  $message = queue_unique_update_8001();
  self::assertStringContainsString('Table dropped', $message);
  self::assertFalse($database_schema
    ->tableExists(UniqueDatabaseQueue::TABLE_NAME));

  // Create the table again and populate it.
  $database_schema
    ->createTable(UniqueDatabaseQueue::TABLE_NAME, $old_schema_definition);
  self::assertTrue($database_schema
    ->tableExists(UniqueDatabaseQueue::TABLE_NAME));
  $names = [
    'testq1',
    'myotherq2',
  ];
  $examples = [
    1,
    '1',
    [
      'a' => 1,
      'b' => [
        'x',
      ],
    ],
    (object) [
      'a' => 1,
      'b' => [
        'x',
      ],
    ],
  ];
  foreach ($names as $name) {
    foreach ($examples as $data) {
      $this
        ->createItemMd5($name, $data);
    }
  }

  /** @var \Drupal\Core\Database\Connection $connection */
  $connection = $this->container
    ->get('database');
  $query = $connection
    ->select(UniqueDatabaseQueue::TABLE_NAME)
    ->countQuery();
  $count = $query
    ->execute()
    ->fetchField();
  self::assertEquals(count($names) * count($examples), $count);
  $message = queue_unique_update_8001();
  $expected = "Migrated {$count} items from the old queue table to the new table.";
  self::assertEquals($expected, $message);
  $queue_factory = $this->container
    ->get('queue_unique.database');
  foreach ($names as $name) {

    /* @var \Drupal\Core\Queue\QueueInterface $queue */
    $queue = $queue_factory
      ->get($name);
    self::assertEquals(count($examples), $queue
      ->numberOfItems());
    foreach ($examples as $data) {

      // If the new hash was calculated correctly by the update, duplicated
      // data cannot be added.
      $duplicate_id = $queue
        ->createItem($data);
      self::assertFalse($duplicate_id);

      // The ordering should have been preserved.
      $item = $queue
        ->claimItem();
      self::assertEquals($data, $item->data);
    }
  }
}