You are here

public function EntityUsageTest::testEntityUsageBlockTrackingHook in Entity Usage 8.3

Same name and namespace in other branches
  1. 8.4 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testEntityUsageBlockTrackingHook()
  2. 8.2 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testEntityUsageBlockTrackingHook()

Tests that our hook correctly blocks a usage from being tracked.

File

tests/src/Kernel/EntityUsageTest.php, line 220

Class

EntityUsageTest
Tests the basic API operations of our tracking service.

Namespace

Drupal\Tests\entity_usage\Kernel

Code

public function testEntityUsageBlockTrackingHook() {

  // The entity_usage_test_entity_usage_block_tracking() implementation is
  // making sure all usage records are blocked when this is set.
  \Drupal::state()
    ->set('entity_usage_kernel_test_block_usage_tracking', TRUE);
  $this->container
    ->get('module_installer')
    ->install([
    'path',
    'views',
    'entity_usage_test',
  ]);
  $entity = $this->testEntities[0];

  /** @var \Drupal\entity_usage\EntityUsage $entity_usage */
  $entity_usage = $this->container
    ->get('entity_usage.usage');
  $entity_usage
    ->registerUsage($entity
    ->id(), $entity
    ->getEntityTypeId(), 1, 'foo', 'en', 0);
  $real_usage = $this->injectedDatabase
    ->select($this->tableName, 'e')
    ->fields('e', [])
    ->condition('e.target_id', $entity
    ->id())
    ->execute()
    ->fetchAll();
  $this
    ->assertEquals(0, count($real_usage));

  // Clean back the environment.
  $this->injectedDatabase
    ->truncate($this->tableName);
  \Drupal::state()
    ->set('entity_usage_kernel_test_block_usage_tracking', FALSE);
}