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