public function EntityUsageTest::testRegisterUsage in Entity Usage 8.3
Same name and namespace in other branches
- 8.4 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testRegisterUsage()
- 8.2 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testRegisterUsage()
Tests the registerUsage() and deleteUsage() methods.
@covers \Drupal\entity_usage\EntityUsage::registerUsage @covers \Drupal\entity_usage\EntityUsage::deleteUsage
File
- tests/
src/ Kernel/ EntityUsageTest.php, line 153
Class
- EntityUsageTest
- Tests the basic API operations of our tracking service.
Namespace
Drupal\Tests\entity_usage\KernelCode
public function testRegisterUsage() {
$entity = $this->testEntities[0];
/** @var \Drupal\entity_usage\EntityUsage $entity_usage */
$entity_usage = $this->container
->get('entity_usage.usage');
// Register a new usage.
$entity_usage
->registerUsage($entity
->id(), $entity
->getEntityTypeId(), 1, 'foo', 'en', 1);
$event = \Drupal::state()
->get('entity_usage_events_test.usage_register', []);
$this
->assertSame($event['event_name'], Events::USAGE_REGISTER);
$this
->assertSame($event['target_id'], $entity
->id());
$this
->assertSame($event['target_type'], $entity
->getEntityTypeId());
$this
->assertSame($event['source_id'], 1);
$this
->assertSame($event['source_type'], 'foo');
$this
->assertSame($event['source_langcode'], 'en');
$this
->assertSame($event['source_vid'], 1);
$real_usage = $this->injectedDatabase
->select($this->tableName, 'e')
->fields('e', [])
->condition('e.target_id', $entity
->id())
->condition('e.target_type', $entity
->getEntityTypeId())
->execute()
->fetchAll();
$this
->assertEquals(1, count($real_usage));
// Check that trying to register an existing record does not fatal out.
$entity_usage
->registerUsage($entity
->id(), $entity
->getEntityTypeId(), 1, 'foo', 'en', 1);
// Delete the record.
$entity_usage
->deleteUsage($entity
->id(), $entity
->getEntityTypeId(), 1, 'foo', 'en', 1);
$real_usage = $this->injectedDatabase
->select($this->tableName, 'e')
->fields('e', [])
->condition('e.target_id', $entity
->id())
->condition('e.target_type', $entity
->getEntityTypeId())
->execute()
->fetchAll();
$this
->assertSame(0, count($real_usage));
// Test that config settings are respected.
$this->container
->get('config.factory')
->getEditable('entity_usage.settings')
->set('track_enabled_target_entity_types', [])
->save();
drupal_flush_all_caches();
$this->container
->get('entity_usage.usage')
->registerUsage($entity
->id(), $entity
->getEntityTypeId(), 1, 'foo', 'en', 1);
$real_usage = $this->injectedDatabase
->select($this->tableName, 'e')
->fields('e', [])
->condition('e.target_id', $entity
->id())
->condition('e.target_type', $entity
->getEntityTypeId())
->execute()
->fetchAll();
$this
->assertSame(0, count($real_usage));
// Clean back the environment.
$this->injectedDatabase
->truncate($this->tableName);
}