public function MessageEntityDelete::testReferencedEntitiesDelete in Message 8
Test deletion of a message after its referenced entities have been deleted.
File
- tests/
src/ Functional/ MessageEntityDelete.php, line 170
Class
- MessageEntityDelete
- Test the Message delete on entity delete functionality.
Namespace
Drupal\Tests\message\FunctionalCode
public function testReferencedEntitiesDelete() {
/** @var \Drupal\Core\Queue\QueueInterface $delete_queue */
$delete_queue = $this->container
->get('queue')
->get('message_delete');
/** @var \Drupal\Core\Queue\QueueInterface $check_delete_queue */
$check_delete_queue = $this->container
->get('queue')
->get('message_check_delete');
/** @var \Drupal\Core\CronInterface $cron */
$cron = $this->container
->get('cron');
// Testing nodes reference.
$message = Message::create([
'template' => 'dummy_message',
]);
$message
->set('field_node_references', [
1,
2,
]);
$message
->save();
Node::load(1)
->delete();
$cron
->run();
$this
->assertTrue(Message::load($message
->id()), 'Message exists after deleting one of two referenced nodes.');
Node::load(2)
->delete();
$cron
->run();
$this
->assertFalse(Message::load($message
->id()), 'Message deleted after deleting all referenced nodes.');
// Test terms references.
$message = Message::create([
'template' => 'dummy_message',
]);
$message
->set('field_term_references', [
1,
2,
]);
$message
->save();
Term::load(1)
->delete();
$cron
->run();
$this
->assertTrue(Message::load($message
->id()), 'Message exists after deleting one of two referenced terms.');
Term::load(2)
->delete();
$cron
->run();
$this
->assertFalse(Message::load($message
->id()), 'Message deleted after deleting all referenced terms.');
// Test term references.
$term = Term::load(3);
$message = Message::create([
'template' => 'dummy_message',
]);
$message
->set('field_term_reference', $term);
$message
->save();
$term
->delete();
$cron
->run();
$this
->assertFalse(Message::load($message
->id()), 'Message deleted after deleting single referenced term.');
// Test node reference.
$message = Message::create([
'template' => 'dummy_message',
]);
$message
->set('field_node_reference', 3);
$message
->save();
Node::load(3)
->delete();
$cron
->run();
$this
->assertFalse(Message::load($message
->id()), 'Message deleted after deleting single referenced node.');
// Testing when a message referenced to terms and term.
$message = Message::create([
'template' => 'dummy_message',
]);
$message
->set('field_term_references', [
4,
5,
]);
$message
->set('field_term_reference', 4);
$message
->save();
Term::load(4)
->delete();
$cron
->run();
$this
->assertFalse(Message::load($message
->id()), 'Message deleted after deleting single referenced term while another the message still references other term in another field.');
// Test user reference.
$account = $this
->drupalCreateUser();
$message = Message::create([
'template' => 'dummy_message',
]);
$message
->set('field_user_reference', $account
->id());
$message
->save();
$account
->delete();
$cron
->run();
$this
->assertFalse(Message::load($message
->id()), 'Message deleted after deleting single referenced user.');
// Test that only messages with a reference to the correct TYPE of entity
// get deleted.
$message_one = Message::create([
'template' => 'dummy_message',
]);
$message_one
->set('field_node_reference', 5);
$message_one
->save();
$message_two = Message::create([
'template' => 'dummy_message',
]);
$message_two
->set('field_term_reference', 5);
$message_two
->save();
Node::load(5)
->delete();
$cron
->run();
$this
->assertFalse(Message::load($message_one
->id()), 'Message with node deleted after deleting referenced node.');
$this
->assertTrue(Message::load($message_two
->id()), 'Message with term remains after deleting a node with the same ID.');
}