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.
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 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.
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.
```language-php
...
$generalUser = $this->drupalCreateUser(array());
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.
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.
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`.
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.
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.
With the refactoring complete, the number of passing tests increased from 142 to 213.