Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Add PHPUnit tests
Functional Javascript Test cases
Logged in as user with "access announcements" permission for first time.
- Alert Icon should display a red dot over it.
- All alerts items should display as unread alerts, having darker background and dot icon to indicate unread.
Logged in as user with "access announcements" permission for second time.
- Alert Icon should not display a red dot over it.
- All alerts icons should display as read alerts, having light background and no dot icon.
Login as another user and access the alert icon
- Alert Icon should display a red dot over it.
- All should be shown as unread
User with out "access announcements" permission.
- No alert icon should display
Add new items to the announce-feed and logged in as a user who has "access announcements" permission.
- Alert Icon should display a red dot over it.
- The new items in the feed should show as unread. Old items should show as read
Access the alert icon again
- Alert Icon should not display a red dot over it.
- All items should show as read
Login as another user and access the alert icon
- Alert Icon should display a red dot over it.
- The new items for that user should be shown as unread
Remove items from the feed
- Removed items should not display in the announcement model
- If the removed item is only item the user hasn't read the the red dot should not show
Empty feed
- Show a message, Feed is empty
Kernel Test cases
Kernel test for the 2 services on the public methods
Issue fork announce-3198730
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
vimaljoseph CreditAttribution: vimaljoseph at Zyxware Technologies for Drupal Association commentedComment #3
tedbowComment #4
tedbowComment #5
tedbowA few suggestions on where to look at core for similar tests.
See this merge request https://git.drupalcode.org/project/drupal/-/merge_requests/284 because it also deals with a single json feed like this module. You can checkout that branch and then look at:
core/modules/system/tests/modules/advisory_feed_test
: This is a test module that serves the feed. Look at the routeadvisory_feed_test.json_test
in this test module for how it serves the json. The actual json files are incore/modules/system/tests/fixtures/psa_feed
You may be able to get away with just directly using the fixtures and not having a controller method like
\Drupal\advisory_feed_test\Controller\AdvisoryTestController::getPsaJson()
. We had to have the controller method there because we need to change the Json files slightly.core/modules/system/src/SecurityAdvisories/SecurityAdvisoriesFetcher.php and search for <code>updates.drupal.org/psa.json
).Using the config URL is easier for testing but it does open the possibility that someone could change the URL to a different json feed. If we only want to support the json feed from drupal.org I would suggest this module also switch to a hard-coded feed URL.
To test the hard-coded feed the test module uses a service decorator on the http_client service.
See:
AdvisoriesTestHttpClient
and the service definition
http_client.advisory_feed_test
The service also has helper method setting the endpoint to test:
\Drupal\advisory_feed_test\AdvisoriesTestHttpClient::setTestEndpoint()
You should be able to copy this test module, do some renames and remove the parts you don't need.
core/modules/system/tests/src/Functional/SecurityAdvisories/SecurityAdvisoryTest.php
is the function test that uses the fake json feed.In your case you will probably want this to be FunctionalJavascript test to opening the announcements dialog.
see
core/modules/workspaces/tests/src/FunctionalJavascript/WorkspaceToolbarIntegrationTest.php
for example of FunctionalJavascript test that opens the off-canvas dialog that this test is also usingannounce.fetcher
andannounce.user_status
This should probably be kernel tests. You can look at
core/modules/system/tests/src/Kernel/SecurityAdvisories/SecurityAdvisoriesFetcherTest.php
in the same merge request. This is similar to what you will need as it tests the public method of the class with mocked feed items. It uses different method than the functional test to provide fake feed items. See\Drupal\Tests\system\Kernel\SecurityAdvisories\SecurityAdvisoriesFetcherTest::setFeedItems()
.Comment #6
mitthukumawat CreditAttribution: mitthukumawat as a volunteer and at Zyxware Technologies for Drupal Association commentedComment #7
mitthukumawat CreditAttribution: mitthukumawat as a volunteer and at Zyxware Technologies for Drupal Association commentedComment #9
vimaljoseph CreditAttribution: vimaljoseph at Zyxware Technologies for Drupal Association commentedCommited the initial tests. Needs review.
Comment #11
tedbowThe test are looking pretty good!
since these have already been committed I created an MR to just had a review of the tests. I have only started with
AlertsJsonFeedTest
but I think some of comments will apply to the other classes too.Let me know if you have questions by leaving MR comments and ping to review the changes
Comment #13
mohit_aghera CreditAttribution: mohit_aghera at QED42 commentedComment #14
mohit_aghera CreditAttribution: mohit_aghera at QED42 for Drupal Association commented@tedbow, I am merging this feedback PR
I am opening up a new PR of the module against Drupal core for the maintainer's feedback.
I'll share the PR here as well, so you can do another round of review in the main PR itself.
Comment #16
Lal_Since the issue is merged, moving this to fixed... Let's work on the core PR if there are any suggestions.