Run prettier on all *.md files
```
prettier '{app,source}/**/**.md' --write
```
This commit is contained in:
parent
a3ceeaf0f3
commit
85a10c545b
170 changed files with 5127 additions and 2282 deletions
|
|
@ -3,19 +3,31 @@ title: Updating Override Node Options Tests
|
|||
date: 2017-05-05
|
||||
excerpt: ~
|
||||
tags:
|
||||
- drupal
|
||||
- drupal-modules
|
||||
- drupal-planet
|
||||
- testing
|
||||
- drupal
|
||||
- drupal-modules
|
||||
- drupal-planet
|
||||
- testing
|
||||
draft: true
|
||||
---
|
||||
Recently, I reviewed [a patch][1] in the [Override Node Options][2] module issue queue. For those not familiar with it, the module adds extra permissions for node options like "authored by" and "published on" which are normally only available to users with the `administer nodes` permission. What the patch does is to optionally add another set of permissions that enable options for all content types - e.g. "override published option for all node types", in addition to or instead of the content type specific ones.
|
||||
|
||||
It was quite an old issue and the latest patch needed to be re-rolled due to merge conflicts, but the existing tests still passed. Though as no new tests were added for the new functionality, these needed to be added before I committed it.
|
||||
Recently, I reviewed [a patch][1] in the [Override Node Options][2] module issue
|
||||
queue. For those not familiar with it, the module adds extra permissions for
|
||||
node options like "authored by" and "published on" which are normally only
|
||||
available to users with the `administer nodes` permission. What the patch does
|
||||
is to optionally add another set of permissions that enable options for all
|
||||
content types - e.g. "override published option for all node types", in addition
|
||||
to or instead of the content type specific ones.
|
||||
|
||||
It was quite an old issue and the latest patch needed to be re-rolled due to
|
||||
merge conflicts, but the existing tests still passed. Though as no new tests
|
||||
were added for the new functionality, these needed to be added before I
|
||||
committed it.
|
||||
|
||||
## Reviewing the Existing Tests
|
||||
|
||||
The first thing to do was to run the existing tests and check that they still passed. I do this on the command line by typing `php scripts/run-tests.sh --class OverrideNodeOptionsTestCase`.
|
||||
The first thing to do was to run the existing tests and check that they still
|
||||
passed. I do this on the command line by typing
|
||||
`php scripts/run-tests.sh --class OverrideNodeOptionsTestCase`.
|
||||
|
||||
```language-markup
|
||||
Drupal test run
|
||||
|
|
@ -35,7 +47,8 @@ Override node options 142 passes, 0 fails, 0 exceptions, and 38 debug messages
|
|||
Test run duration: 32 sec
|
||||
```
|
||||
|
||||
After confirming that the existing tests still passed, I reviewed them to see what could be re-used.
|
||||
After confirming that the existing tests still passed, I reviewed them to see
|
||||
what could be re-used.
|
||||
|
||||
This is one of the original tests:
|
||||
|
||||
|
|
@ -68,9 +81,14 @@ protected function testNodeOptions() {
|
|||
}
|
||||
```
|
||||
|
||||
The first part of the test is creating and logging in a user with some content type specific override permissions (`$this->adminUser`), and then testing that the fields were updated when the node is saved. The second part is testing that the fields are not visible for a normal user without the extra permissions (`$this->normalUser`), which is created in the `setUp()` class' method.
|
||||
The first part of the test is creating and logging in a user with some content
|
||||
type specific override permissions (`$this->adminUser`), and then testing that
|
||||
the fields were updated when the node is saved. The second part is testing that
|
||||
the fields are not visible for a normal user without the extra permissions
|
||||
(`$this->normalUser`), which is created in the `setUp()` class' method.
|
||||
|
||||
To test the new "all types" permissions, I created another user to test against called `$generalUser` and run the first part of the tests in a loop.
|
||||
To test the new "all types" permissions, I created another user to test against
|
||||
called `$generalUser` and run the first part of the tests in a loop.
|
||||
|
||||
## Beginning to Refactor the Tests
|
||||
|
||||
|
|
@ -89,19 +107,21 @@ $specificUser = $this->drupalCreateUser(array(
|
|||
|
||||
foreach (array($specificUser) as $account) {
|
||||
$this->drupalLogin($account);
|
||||
|
||||
|
||||
// Test all the things.
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
I started with a small change, renaming `$this->adminUser` to `$specificUser` to make it clearer what permissions it had, and moving the tests into a loop so that the tests can be repeated for both users.
|
||||
I started with a small change, renaming `$this->adminUser` to `$specificUser` to
|
||||
make it clearer what permissions it had, and moving the tests into a loop so
|
||||
that the tests can be repeated for both users.
|
||||
|
||||
After that change, I ran the tests again to check that everything still worked.
|
||||
|
||||
## Adding Failing Tests
|
||||
|
||||
The next step is to start testing the new permissions.
|
||||
The next step is to start testing the new permissions.
|
||||
|
||||
```language-php
|
||||
...
|
||||
|
|
@ -115,7 +135,9 @@ foreach (array($specificUser, $generalUser) as $account) {
|
|||
}
|
||||
```
|
||||
|
||||
I added a new `$generalUser` to test the general permissions and added to the loop, but in order to see the tests failing intially I assigned it no permissions. When running the tests again, 6 tests have failed.
|
||||
I added a new `$generalUser` to test the general permissions and added to the
|
||||
loop, but in order to see the tests failing intially I assigned it no
|
||||
permissions. When running the tests again, 6 tests have failed.
|
||||
|
||||
```language-markup
|
||||
Test summary
|
||||
|
|
@ -126,13 +148,18 @@ Override node options 183 passes, 6 fails, 0 exceptions, and 49 debug messages
|
|||
Test run duration: 28 sec
|
||||
```
|
||||
|
||||
Then it was a case of re-adding more permissions to the user and seeing the number of failures decrease, confirming that the functionality was working correctly.
|
||||
Then it was a case of re-adding more permissions to the user and seeing the
|
||||
number of failures decrease, confirming that the functionality was working
|
||||
correctly.
|
||||
|
||||
TODO: Add another example.
|
||||
|
||||
## Gotchas
|
||||
|
||||
There was a bug that I found where a permission was added, but wasn't used within the implementation code. After initially expecting the test to pass after adding the permission to `$generalUser` and the test still failed, I noticed that the
|
||||
There was a bug that I found where a permission was added, but wasn't used
|
||||
within the implementation code. After initially expecting the test to pass after
|
||||
adding the permission to `$generalUser` and the test still failed, I noticed
|
||||
that the
|
||||
|
||||
This was fixed by adding the extra code into `override_node_options.module`.
|
||||
|
||||
|
|
@ -141,9 +168,12 @@ This was fixed by adding the extra code into `override_node_options.module`.
|
|||
+ $form['comment_settings']['#access'] |= user_access('override ' . $node->type . ' comment setting option') || user_access('override all comment setting option');
|
||||
```
|
||||
|
||||
The other issue that I found was within `testNodeRevisions`. `assertNodeFieldsUpdated()` was failing after being put in a loop as the `vid` was not the same as what was expected.
|
||||
The other issue that I found was within `testNodeRevisions`.
|
||||
`assertNodeFieldsUpdated()` was failing after being put in a loop as the `vid`
|
||||
was not the same as what was expected.
|
||||
|
||||
Note: You can get more verbose output from `run-tests.sh` by adding the `--verbose` option.
|
||||
Note: You can get more verbose output from `run-tests.sh` by adding the
|
||||
`--verbose` option.
|
||||
|
||||
> Node vid was updated to '3', expected 2.
|
||||
|
||||
|
|
@ -173,16 +203,25 @@ Note: You can get more verbose output from `run-tests.sh` by adding the `--verbo
|
|||
+ }
|
||||
```
|
||||
|
||||
The crucial part of this change was the addition of `$node = node_load($this->node->nid, NULL, TRUE);` to ensure that the latest version of the node was loaded during each loop.
|
||||
The crucial part of this change was the addition of
|
||||
`$node = node_load($this->node->nid, NULL, TRUE);` to ensure that the latest
|
||||
version of the node was loaded during each loop.
|
||||
|
||||
## Conclusion
|
||||
|
||||
- Ensure that the existing tests were passing before starting to refactor.
|
||||
- Start with small changes and continue to run the tests to ensure that nothing has broken.
|
||||
- After the first change, I committed it as `WIP: Refactoring tests`, and used `git commit --amend --no-edit` to amend that commit each time I had refactored another test. After the last refactor, I updated the commit message.
|
||||
- It’s important to see tests failing before making them pass. This was achieved by initially assigning no permissions to `$generalUser` so that the fails failed and then added permissions and re-run the tests to ensure that the failure count decreased with each new permission.
|
||||
- Start with small changes and continue to run the tests to ensure that nothing
|
||||
has broken.
|
||||
- After the first change, I committed it as `WIP: Refactoring tests`, and used
|
||||
`git commit --amend --no-edit` to amend that commit each time I had refactored
|
||||
another test. After the last refactor, I updated the commit message.
|
||||
- It’s important to see tests failing before making them pass. This was achieved
|
||||
by initially assigning no permissions to `$generalUser` so that the fails
|
||||
failed and then added permissions and re-run the tests to ensure that the
|
||||
failure count decreased with each new permission.
|
||||
|
||||
With the refactoring complete, the number of passing tests increased from 142 to 213.
|
||||
With the refactoring complete, the number of passing tests increased from 142
|
||||
to 213.
|
||||
|
||||
```language-markup
|
||||
Override node options 213 passes, 0 fails, 0 exceptions, and 60 debug messages
|
||||
|
|
@ -192,7 +231,8 @@ Test run duration: 25 sec
|
|||
|
||||
<img src="/images/blog/override-node-options-refactor-tests-new-passing.png" alt="">
|
||||
|
||||
[Here][3] are my full changes from the previous patch, where I added the new tests as well as some small refactors.
|
||||
[Here][3] are my full changes from the previous patch, where I added the new
|
||||
tests as well as some small refactors.
|
||||
|
||||
[1]: https://www.drupal.org/node/974730
|
||||
[2]: https://www.drupal.org/project/override_node_options
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue