Problem/Motivation

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.

Steps to reproduce

Currently, tests have been added only for the administration functionality.

Proposed resolution

Write tests for these 2 functionalities.

Issue fork disqus-3260593

Command icon 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

gaurav.kapoor created an issue. See original summary.

gaurav.kapoor’s picture

Issue tags: +Needs tests
gaurav.kapoor’s picture

Title: Write tests for » Write tests for callback in NewCommentController and NewCommentEventSubscriber.
subhojit777’s picture

Sharing 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.

gaurav.kapoor’s picture

Assigned: Unassigned » gaurav.kapoor
gaurav.kapoor’s picture

The 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.

gaurav.kapoor’s picture

Status: Active » Needs review

Added a very simple test and did a few changes in the NewCommentController to make sure tests pass.