-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VACMS-18005: Aging content tests (#18256)
* VACMS-18005: Adds tests for JobTypeMessageNotifyBase. * VACMS-18005: Adds missing template exception to the test. * VACMS-18005: Adds functional tests of expired or warned FWB. * VACMS-18005: Updates expirable_content to latest alpha. --------- Co-authored-by: Tim Cosgrove <[email protected]> Co-authored-by: Tim Cosgrove <[email protected]>
- Loading branch information
1 parent
573ad4e
commit 2b19977
Showing
4 changed files
with
258 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
tests/phpunit/va_gov_notifications/functional/AgingContentFullWidthBannerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace tests\phpunit\va_gov_notifications\functional; | ||
|
||
use Drupal\Core\Datetime\DrupalDateTime; | ||
use Drupal\Core\StringTranslation\StringTranslationTrait; | ||
use JetBrains\PhpStorm\ArrayShape; | ||
use Tests\Support\Classes\VaGovExistingSiteBase; | ||
|
||
/** | ||
* Tests Aging Content FWB notifications. | ||
* | ||
* @group va_gov_notifications | ||
* @group aging_content | ||
*/ | ||
class AgingContentFullWidthBannerTest extends VaGovExistingSiteBase { | ||
|
||
use StringTranslationTrait; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setUp(): void { | ||
parent::setUp(); | ||
$user = $this->createUser([], 'Admin', TRUE); | ||
$this->drupalLogin($user); | ||
|
||
$node_base = [ | ||
'title' => 'Expirable Content Test Node', | ||
'status' => 1, | ||
'moderation_state' => 'published', | ||
'type' => 'banner', | ||
'uid' => 1, | ||
'revision_default' => 1, | ||
'field_administration' => ['target_id' => 194], | ||
]; | ||
|
||
$base_date = DrupalDateTime::createFromTimestamp(time()); | ||
$warn_node = clone $base_date; | ||
$exp_node = clone $base_date; | ||
|
||
$dates = [ | ||
'not expired or warn' => $base_date->getTimestamp(), | ||
'warn' => $warn_node->sub(new \DateInterval('P5D'))->getTimestamp(), | ||
'expired' => $exp_node->sub(new \DateInterval('P8D'))->getTimestamp(), | ||
]; | ||
|
||
foreach ($dates as $key => $date) { | ||
$node = $this->createNode($node_base); | ||
$node->set('field_last_saved_by_an_editor', $date); | ||
$node->set('title', $node->getTitle() . ':' . $key); | ||
$node->save(); | ||
} | ||
} | ||
|
||
/** | ||
* Data provider for expiration and warning tests. | ||
*/ | ||
#[ArrayShape(['expired' => "string[]", 'warn' => "string[]"])] | ||
public function dataProvider(): array { | ||
return [ | ||
'expired' => ['expired'], | ||
'warn' => ['warn'], | ||
]; | ||
} | ||
|
||
/** | ||
* Tests that Full Width Alerts (banner) notifications are sent. | ||
* | ||
* @param string $type | ||
* The type of test either 'warn' or 'expired'. | ||
* | ||
* @dataProvider dataProvider | ||
* | ||
* @throws \Behat\Mink\Exception\ResponseTextException | ||
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException | ||
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException | ||
* @throws \Drupal\Core\Entity\EntityStorageException | ||
*/ | ||
public function testFullWidthBannerJobQueued(string $type) { | ||
// Enable the ECA if it is not already. | ||
/** @var \Drupal\eca\Entity\Eca $eca */ | ||
$eca = \Drupal::entityTypeManager()->getStorage('eca')->load("aging_content_{$type}_fwb"); | ||
$status = $eca->status(); | ||
$eca->enable(); | ||
$eca->save(); | ||
|
||
// Run cron to queue the job. | ||
$this->drupalGet('admin/reports/status'); | ||
$this->clickLink($this->t('Run cron')); | ||
|
||
// Check that the job is queued. | ||
$this->drupalGet('admin/config/system/queues/jobs/aging_content'); | ||
$this->assertSession()->pageTextContains("Expirable Content Test Node:{$type}"); | ||
|
||
// Set ECA to previous state. This is to prevent duplicate queued items. | ||
$eca->setStatus($status); | ||
$eca->save(); | ||
|
||
// Run cron again to execute the job, which sends the notification. | ||
$this->drupalGet('admin/reports/status'); | ||
$this->clickLink($this->t('Run cron')); | ||
|
||
$this->assertSession()->pageTextContains('Success'); | ||
} | ||
|
||
} |
138 changes: 138 additions & 0 deletions
138
tests/phpunit/va_gov_notifications/kernel/JobTypeMessageNotifyBaseTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\va_gov_notifications\kernel; | ||
|
||
use Drupal\advancedqueue\Job; | ||
use Drupal\Core\TypedData\Exception\MissingDataException; | ||
use Drupal\KernelTests\KernelTestBase; | ||
use Drupal\message\Entity\MessageTemplate; | ||
use Drupal\Tests\user\Traits\UserCreationTrait; | ||
use Drupal\va_gov_notifications\JobTypeMessageNotifyBase; | ||
|
||
/** | ||
* Tests for JobTypeMessageNotifyBase. | ||
* | ||
* @group va_gov_notifications | ||
* @covers JobTypeMessageNotifyBase | ||
*/ | ||
class JobTypeMessageNotifyBaseTest extends KernelTestBase { | ||
|
||
use UserCreationTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static $modules = [ | ||
'system', | ||
'user', | ||
'message', | ||
'message_notify', | ||
'va_gov_notifications', | ||
'advancedqueue', | ||
'flag', | ||
'workbench_access', | ||
]; | ||
|
||
/** | ||
* The job type plugin under test. | ||
* | ||
* @var \Drupal\va_gov_notifications\JobTypeMessageNotifyBase | ||
*/ | ||
protected JobTypeMessageNotifyBase $jobType; | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws \Drupal\Core\Entity\EntityStorageException | ||
*/ | ||
protected function setUp(): void { | ||
parent::setUp(); | ||
$this->installEntitySchema('user'); | ||
$this->installEntitySchema('message'); | ||
$this->createUser(admin: FALSE); | ||
|
||
// Create message template. | ||
$message_template = MessageTemplate::create([ | ||
'template' => 'foo_template', | ||
'label' => 'Example Template', | ||
]); | ||
$message_template->save(); | ||
|
||
// Instantiate the job type. | ||
$this->jobType = new JobTypeMessageNotifyBase( | ||
['id' => 'test_job_type'], | ||
'test_job_type', | ||
['provider' => 'va_gov_notifications'], | ||
$this->container->get('logger.factory'), | ||
$this->container->get('message_notify.sender') | ||
); | ||
} | ||
|
||
/** | ||
* Tests the process method for a successful message send. | ||
*/ | ||
public function testProcessSuccess() { | ||
$payload = [ | ||
'values' => [], | ||
'template_values' => [ | ||
'uid' => 1, | ||
'template' => 'foo_template', | ||
], | ||
'restrict_delivery_to' => [1], | ||
]; | ||
$job = new Job([ | ||
'type' => 'test_job_type', | ||
'payload' => $payload, | ||
'state' => Job::STATE_QUEUED, | ||
]); | ||
|
||
$result = $this->jobType->process($job); | ||
$this->assertEquals(Job::STATE_SUCCESS, $result->getState()); | ||
$this->assertEquals('Message 1 sent successfully.', $result->getMessage()); | ||
} | ||
|
||
/** | ||
* Tests the process method with an error due to missing template values. | ||
*/ | ||
public function testProcessFailureRecipientAllowList() { | ||
$payload = [ | ||
'values' => [], | ||
'template_values' => [ | ||
'uid' => 1, | ||
'template' => 'foo_template', | ||
], | ||
'restrict_delivery_to' => [2], | ||
]; | ||
$job = new Job([ | ||
'type' => 'test_job_type', | ||
'payload' => $payload, | ||
'state' => Job::STATE_QUEUED, | ||
]); | ||
|
||
$result = $this->jobType->process($job); | ||
$this->assertEquals(Job::STATE_FAILURE, $result->getState()); | ||
$this->assertEquals('Recipient is not on the allow list for message 1.', $result->getMessage()); | ||
} | ||
|
||
/** | ||
* Tests the process method with an error due to missing template values. | ||
*/ | ||
public function testProcessFailureMissingTemplateValues() { | ||
$payload = [ | ||
'values' => [], | ||
]; | ||
$job = new Job([ | ||
'type' => 'test_job_type', | ||
'payload' => $payload, | ||
'state' => Job::STATE_QUEUED, | ||
]); | ||
$this->expectException(MissingDataException::class); | ||
$this->expectExceptionMessage('Missing template_values in payload for job id'); | ||
$result = $this->jobType->process($job); | ||
$this->assertEquals(Job::STATE_FAILURE, $result->getState()); | ||
|
||
} | ||
|
||
} |