public function EntityUsageTest::testDeleteByTargetEntity 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::testDeleteByTargetEntity()
- 8.2 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testDeleteByTargetEntity()
Tests the deleteByTargetEntity() method.
@covers \Drupal\entity_usage\EntityUsage::deleteByTargetEntity
File
- tests/
src/ Kernel/ EntityUsageTest.php, line 402
Class
- EntityUsageTest
- Tests the basic API operations of our tracking service.
Namespace
Drupal\Tests\entity_usage\KernelCode
public function testDeleteByTargetEntity() {
// Create 2 fake registers on the database table, one for each entity.
$i = 0;
foreach ($this->testEntities as $entity) {
$i++;
$this->injectedDatabase
->insert($this->tableName)
->fields([
'target_id' => $entity
->id(),
'target_type' => $entity
->getEntityTypeId(),
'source_id' => $i,
'source_type' => 'fake_type_' . $i,
'source_langcode' => 'en',
'source_vid' => $i,
])
->execute();
}
/** @var \Drupal\entity_usage\EntityUsage $entity_usage */
$entity_usage = $this->container
->get('entity_usage.usage');
// Delete only one of them, by target.
$entity_usage
->deleteByTargetEntity($this->testEntities[0]
->id(), $this->testEntities[0]
->getEntityTypeId());
$event = \Drupal::state()
->get('entity_usage_events_test.usage_delete_by_target_entity', []);
$this
->assertSame($event['event_name'], Events::DELETE_BY_TARGET_ENTITY);
$this
->assertSame($event['target_id'], $this->testEntities[0]
->id());
$this
->assertSame($event['target_type'], $this->testEntities[0]
->getEntityTypeId());
$this
->assertSame($event['source_id'], NULL);
$this
->assertSame($event['source_type'], NULL);
$this
->assertSame($event['source_langcode'], NULL);
// The non-affected record is still there.
$real_source_list = $entity_usage
->listSources($this->testEntities[1]);
$expected_source_list = [
'fake_type_2' => [
'2' => [
0 => [
'source_langcode' => 'en',
'source_vid' => '2',
],
],
],
];
$this
->assertEquals($expected_source_list, $real_source_list);
// The affected record is gone.
$real_source_list = $entity_usage
->listSources($this->testEntities[0]);
$this
->assertEquals([], $real_source_list);
// Clean back the environment.
$this->injectedDatabase
->truncate($this->tableName);
}