diff --git a/src/Entity/Digest.php b/src/Entity/Digest.php index 084b9c7..2bd0da3 100644 --- a/src/Entity/Digest.php +++ b/src/Entity/Digest.php @@ -91,13 +91,6 @@ class Digest extends ConfigEntityBase implements DigestInterface { */ protected $send_at; - /** - * A loaded CronExpression instance of the 'send_at' value. - * - * @var \Cron\CronExpression - */ - private $send_at_instance; - /** * The ID block used to create the body of this digest. * @@ -105,13 +98,6 @@ class Digest extends ConfigEntityBase implements DigestInterface { */ protected $display_block; - /** - * The loaded instance of the display block for this digest. - * - * @var \Drupal\Core\Block\BlockPluginInterface - */ - private $display_block_instance; - /** * The block plugin manager. * @@ -188,12 +174,12 @@ class Digest extends ConfigEntityBase implements DigestInterface { */ public function getSendAt() { - // Load the CronExpression if it has not already been loaded. - if (!isset($this->send_at_instance)) { - $this->send_at_instance = new CronExpression($this->send_at); + try { + return new CronExpression($this->send_at); + } + catch (\Throwable $exception) { + return NULL; } - - return $this->send_at_instance; } @@ -210,7 +196,6 @@ class Digest extends ConfigEntityBase implements DigestInterface { } $this->send_at = $setTo->getExpression(); - $this->send_at_instance = $setTo; return $this; @@ -220,15 +205,7 @@ class Digest extends ConfigEntityBase implements DigestInterface { * {@inheritDoc} */ public function getDisplayBlock() { - - // Load the block if it has not already been loaded. - if (!isset($this->display_block_instance)) { - $this->display_block_instance = - $this->blockManager->createInstance($this->display_block); - } - - return $this->display_block_instance; - + return $this->blockManager->createInstance($this->display_block); } /** @@ -242,7 +219,6 @@ class Digest extends ConfigEntityBase implements DigestInterface { // Update the block ID and instance. $this->display_block = $setTo; - $this->display_block_instance = $this->blockManager->createInstance($setTo); return $this; diff --git a/src/Entity/DigestInterface.php b/src/Entity/DigestInterface.php index 9912483..9bf74f5 100644 --- a/src/Entity/DigestInterface.php +++ b/src/Entity/DigestInterface.php @@ -68,8 +68,9 @@ interface DigestInterface extends ConfigEntityInterface { /** * Returns the current value of 'send_at' as a CronExpression. * - * @return \Cron\CronExpression + * @return \Cron\CronExpression|null * The CronExpression representation of 'send_at'. + * Returns NULL if no send at value is invalid or was not set. */ public function getSendAt(); @@ -92,6 +93,8 @@ interface DigestInterface extends ConfigEntityInterface { * * @return \Drupal\Core\Block\BlockPluginInterface * The block plugin for this digest. + * If the value for the display block ID is invalid the 'broken' block will + * be returned. */ public function getDisplayBlock(); diff --git a/tests/src/Kernel/Entity/DigestTest.php b/tests/src/Kernel/Entity/DigestTest.php index 7052091..a1269b3 100644 --- a/tests/src/Kernel/Entity/DigestTest.php +++ b/tests/src/Kernel/Entity/DigestTest.php @@ -114,30 +114,43 @@ class DigestTest extends EntityKernelTestBase { public function testSendAt() { $valid_cron = '* * * * *'; - $invalid_cron = 'I am not cron'; + + // Getting the send at before it has been set should return null. + $this->assertNull($this->digest->getSendAt()); + + // An invalid cron value should also return NULL. + $this->digest->set('send_at', 'Not cron'); + $this->assertNull($this->digest->getSendAt()); // Should successfully set and retrieve a cron expression. $this->digest->setSendAt($valid_cron); $expression = $this->digest->getSendAt()->getExpression(); $this->assertEqual($valid_cron, $expression); - // An exception should be thrown for invalid cron strings. - $this->expectException(\InvalidArgumentException::class); - $this->digest->setSendAt($invalid_cron); + try { - // The value of send at should not have changed. - $expression = $this->digest->getSendAt()->getExpression(); - $this->assertEqual($valid_cron, $expression); + // An exception should be thrown for invalid cron strings. + $this->digest->setSendAt('I am not cron'); + $this->fail('No exception thrown for invalid cron expression.'); - // Reset the digest. - $this->digest = $this->digestStorage->create([ - 'id' => 'my_digest', - ]); - $this->digest->save(); + } + catch (\InvalidArgumentException $exception) { + + // The value of send at should not have changed. + $expression = $this->digest->getSendAt()->getExpression(); + $this->assertEqual($valid_cron, $expression); + + } + + // The correct expression should be returned after updating the value + // through generic entity methods. + $valid_cron = '0 0 * * 5'; + $this->digest->set('send_at', $valid_cron); + $expression = $this->digest->getSendAt(); + $this->assertEqual($valid_cron, $expression); // The same results should occur if a CronExpression object is passed in. $cron_expression = new CronExpression($valid_cron); - $this->digest->setSendAt($cron_expression); $expression = $this->digest->getSendAt(); $this->assertEqual($cron_expression, $expression); @@ -154,15 +167,30 @@ class DigestTest extends EntityKernelTestBase { $valid_block = 'page_title_block'; + // Should successfully set and retrieve the display block. $this->digest->setDisplayBlock($valid_block); $block = $this->digest->getDisplayBlock(); $this->assertEqual($valid_block, $block->getPluginId()); - // An exception should be thrown for invalid IDs. - $this->expectException(PluginException::class); - $this->digest->setDisplayBlock('Not a block'); + try { + + // An exception should be thrown for invalid IDs. + $this->digest->setDisplayBlock('Not a block'); + $this->fail('No exception thrown for invalid block ID.'); + + } + catch (PluginException $exception) { + + // The value of send at should not have changed. + $block = $this->digest->getDisplayBlock(); + $this->assertEqual($valid_block, $block->getPluginId()); + + } - // The value of send at should not have changed. + // The correct block should be returned after updating the value through + // generic entity methods. + $valid_block = 'user_login_block'; + $this->digest->set('display_block', $valid_block); $block = $this->digest->getDisplayBlock(); $this->assertEqual($valid_block, $block->getPluginId());