public function EntityUsageTest::testRemoveUsage in Entity Usage 8
Tests the delete() method.
@covers \Drupal\entity_usage\EntityUsage::delete
File
- tests/
src/ Kernel/ EntityUsageTest.php, line 170
Class
- EntityUsageTest
- Tests the basic API operations of our tracking service..
Namespace
Drupal\Tests\entity_usage\KernelCode
public function testRemoveUsage() {
$entity = $this->testEntities[0];
/** @var \Drupal\entity_usage\EntityUsage $entity_usage */
$entity_usage = $this->container
->get('entity_usage.usage');
$this->injectedDatabase
->insert($this->tableName)
->fields([
't_id' => $entity
->id(),
't_type' => $entity
->getEntityTypeId(),
're_id' => 1,
're_type' => 'foo',
'method' => 'entity_reference',
'count' => 3,
])
->execute();
// Normal decrement.
$entity_usage
->delete($entity
->id(), $entity
->getEntityTypeId(), 1, 'foo', 1);
$event = \Drupal::state()
->get('entity_usage_events_test.usage_delete', []);
$this
->assertSame($event['event_name'], Events::USAGE_DELETE);
$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'], NULL);
$this
->assertSame($event['count'], 1);
$count = $this->injectedDatabase
->select($this->tableName, 'e')
->fields('e', [
'count',
])
->condition('e.t_id', $entity
->id())
->condition('e.t_type', $entity
->getEntityTypeId())
->execute()
->fetchField();
$this
->assertEquals(2, $count, 'The count was decremented correctly.');
// Multiple decrement and removal.
$entity_usage
->delete($entity
->id(), $entity
->getEntityTypeId(), 1, 'foo', 2);
$count = $this->injectedDatabase
->select($this->tableName, 'e')
->fields('e', [
'count',
])
->condition('e.t_id', $entity
->id())
->condition('e.t_type', $entity
->getEntityTypeId())
->execute()
->fetchField();
$this
->assertSame(FALSE, $count, 'The count was removed entirely when empty.');
// Non-existent decrement.
$entity_usage
->delete($entity
->id(), $entity
->getEntityTypeId(), 1, 'foo', 2);
$count = $this->injectedDatabase
->select($this->tableName, 'e')
->fields('e', [
'count',
])
->condition('e.t_id', $entity
->id())
->condition('e.t_type', $entity
->getEntityTypeId())
->execute()
->fetchField();
$this
->assertSame(FALSE, $count, 'Decrementing non-existing record complete.');
// Clean back the environment.
$this->injectedDatabase
->truncate($this->tableName);
}