You are here

public function EntityUsageTest::testAddUsage in Entity Usage 8

Tests the add() method.

@covers \Drupal\entity_usage\EntityUsage::add

File

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

Class

EntityUsageTest
Tests the basic API operations of our tracking service..

Namespace

Drupal\Tests\entity_usage\Kernel

Code

public function testAddUsage() {
  $entity = $this->testEntities[0];

  /** @var \Drupal\entity_usage\EntityUsage $entity_usage */
  $entity_usage = $this->container
    ->get('entity_usage.usage');
  $entity_usage
    ->add($entity
    ->id(), $entity
    ->getEntityTypeId(), '1', 'foo', 'entity_reference', 1);
  $event = \Drupal::state()
    ->get('entity_usage_events_test.usage_add', []);
  $this
    ->assertSame($event['event_name'], Events::USAGE_ADD);
  $this
    ->assertSame($event['target_id'], $entity
    ->id());
  $this
    ->assertSame($event['target_type'], $entity
    ->getEntityTypeId());
  $this
    ->assertSame($event['referencing_id'], '1');
  $this
    ->assertSame($event['referencing_type'], 'foo');
  $this
    ->assertSame($event['method'], 'entity_reference');
  $this
    ->assertSame($event['count'], 1);
  $real_usage = $this->injectedDatabase
    ->select($this->tableName, 'e')
    ->fields('e', [
    'count',
  ])
    ->condition('e.t_id', $entity
    ->id())
    ->execute()
    ->fetchField();
  $this
    ->assertEquals(1, $real_usage, 'Usage saved correctly to the database.');

  // Clean back the environment.
  $this->injectedDatabase
    ->truncate($this->tableName);
}