public function EntityUsageTest::testRegisterUsage in Entity Usage 8.2
Same name and namespace in other branches
- 8.4 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testRegisterUsage()
- 8.3 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testRegisterUsage()
Tests the registerUsage() method.
@covers \Drupal\entity_usage\EntityUsage::registerUsage
File
- tests/
src/ Kernel/ EntityUsageTest.php, line 264
Class
- EntityUsageTest
- Tests the basic API operations of our tracking service.
Namespace
Drupal\Tests\entity_usage\KernelCode
public function testRegisterUsage() {
$entity = $this->testEntities[0];
$field_name = 'body';
/** @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, 'entity_reference', $field_name, 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);
$this
->assertSame($event['method'], 'entity_reference');
$this
->assertSame($event['field_name'], $field_name);
$this
->assertSame($event['count'], 1);
$real_usage = $this->injectedDatabase
->select($this->tableName, 'e')
->fields('e', [
'count',
])
->condition('e.target_id', $entity
->id())
->condition('e.target_type', $entity
->getEntityTypeId())
->execute()
->fetchField();
$this
->assertEquals(1, $real_usage);
// Delete the record.
$entity_usage
->registerUsage($entity
->id(), $entity
->getEntityTypeId(), 1, 'foo', 'en', 1, 'entity_reference', $field_name, 0);
$real_usage = $this->injectedDatabase
->select($this->tableName, 'e')
->fields('e', [
'count',
])
->condition('e.target_id', $entity
->id())
->condition('e.target_type', $entity
->getEntityTypeId())
->execute()
->fetchField();
$this
->assertSame(FALSE, $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, 'entity_reference', $field_name, 1);
$real_usage = $this->injectedDatabase
->select($this->tableName, 'e')
->fields('e', [
'count',
])
->condition('e.target_id', $entity
->id())
->condition('e.target_type', $entity
->getEntityTypeId())
->execute()
->fetchField();
$this
->assertSame(FALSE, $real_usage);
// Clean back the environment.
$this->injectedDatabase
->truncate($this->tableName);
}