Most of the assert methods in VoteTest are used wrongly, and some are used in an extremely confusing/messed-up way.

Take for example:
assertFalse(empty($results['vote']), 'Results for test vote type are available.')
Because empty()returns a boolean, this is checking that the array element is not empty. So we should be using assertNotEmpty() instead. like this:
assertNotEmpty($results['vote'], 'Results for test vote type are available.');
I think you will agree that this is easier to understand and clearer about what is being tested.

and that's not even a confusing one. Try to understand this:
assertNotEmpty(empty($results['test']['vote_count']), 'Result removed via alter hook was not calculated.');
Again, empty()returns a boolean, so by using assertNotEmpty() like this we're checking to see if empty() is TRUE. In other words, this should really be written as:
assertEmpty($results['test']['vote_count'], 'Result removed via alter hook was not calculated.');
The current usage is the code equivalent of a double negative.

There are a bunch of these.

This patch also fixes the misuse of assertEquals(). The first two arguments to this function should be 1) the expected value, followed by 2) the actual value. ALL uses of assertEquals() in this test have these arguments reversed, probably because this code was ported from using the Simpletest assertEqual() (without the trailing 's'!) which uses the same arguments in reverse.

Fixing this is important for readability because all PHPUnit asserts have the expected value as the first argument.

This patch also changes a few assertEquals() to assertCount(). For example:
assertEquals(count($votes), 0, 'When an entity is deleted, the votes are also deleted.');
Becomes:
assertCount(0, $votes, 'When an entity is deleted, the votes are also deleted.');

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TR created an issue. See original summary.

TR’s picture

Status: Active » Needs review
TR’s picture

FileSize
6.9 KB

Whoops, I added one coding standards problem. Here's a new patch with that fixed.

TR’s picture

This patch will have to be re-rolled after #2592875: Module override of result set isn't working and #3061823: Implement hook_votingapi_results_alter are committed. Both of those should be committed FIRST since they are fixing real problems. This current issue is more of a clean-up task to make things easier to understand and maintain.

pifagor’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
TR’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
7.23 KB
1.43 KB
TR’s picture

FileSize
7.37 KB
1.42 KB

I cancelled the test in #6 because the patch was wrong. Here's the correct patch and interdiff.

Krzysztof Domański’s picture

Status: Needs review » Reviewed & tested by the community

Nice improvement!

  • pifagor committed b62ccc4 on 8.x-3.x authored by TR
    Issue #3142786 by TR, pifagor, Krzysztof Domański: Fix highly confusing...
pifagor’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.