Needs review
Project:
Disqus
Version:
2.0.x-dev
Component:
Code
Priority:
Normal
Category:
Task
Assigned:
Issue tags:
Reporter:
Created:
26 Jan 2022 at 12:59 UTC
Updated:
1 Feb 2022 at 12:18 UTC
Jump to comment: Most recent
Based on the MR feedback provided here, we should add tests for validating the behavior of the receiver callback in NewCommentController and validating the behavior of NewCommentEventSubscriber. These were introduced in this issue.
Currently, tests have been added only for the administration functionality.
Write tests for these 2 functionalities.
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
gaurav.kapoor commentedComment #3
gaurav.kapoor commentedComment #4
subhojit777Sharing some suggestions how to proceed with the tests.
The test for `NewCommentController` can be a functional test. There are lots of if-else blocks. So, the test should validate the behavior when a certain condition is true. Better add negative test cases as well that will validate the code written inside the `else` block. Let's say that you want to write a test case for the below code snippet taken from `NewCommentController`:
```
if ($this->flood->isAllowed('disqus.new_comment', $limit, $interval)) {
// Register a flood event; but it will be cleared if the request turns out
// to be genuine.
$this->flood->register('disqus.new_comment', $interval);
}
else {
return new Response('', 400);
}
```
0. Prepare some test comment, and flood configs.
1. Hit this endpoint `/disqus/new-comment/{comment_id}` such that `$this->flood->isAllowed('disqus.new_comment', $limit, $interval)` is true
2. Validate whether a record has been entered in the database. Basically validating this: `\Drupal\Core\Flood\DatabaseBackend::doInsert`
And, as a negative test case:
1. Hit this endpoint `/disqus/new-comment/{comment_id}` such that `$this->flood->isAllowed('disqus.new_comment', $limit, $interval)` is false
2. Validate that the page response is 400
---
Similarly, `NewCommentEventSubscriber::onNewComment` can also be a functionally. Try to create the action in test that is supposed to trigger event `NewCommentEvent::NEW_COMMENT`.
It might be tricky to have 100% test coverage of the methods. Try to have as much code coverage as possible.
Comment #5
gaurav.kapoor commentedComment #6
gaurav.kapoor commentedThe New Comment Controller requires better error handling before a test can be written. Currently, it throws a 500 error if accessed without configuring a secret key. We also have to think about mocking the Disqus API to write meaningful tests in this case.
Comment #8
gaurav.kapoor commentedAdded a very simple test and did a few changes in the NewCommentController to make sure tests pass.