API page: https://api.drupal.org/api/drupal/core!modules!node!lib!Drupal!node!Test...
This test expects the node creation to throw an exception, but there's no visible reason why it would do that:
function testFailedPageCreation() {
// Create a node.
$edit = array(
'uid' => $this->loggedInUser->id(),
'name' => $this->loggedInUser->name,
'type' => 'page',
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
'title' => 'testing_transaction_exception',
);
try {
entity_create('node', $edit)->save();
$this->fail(t('Expected exception has not been thrown.'));
}
catch (\Exception $e) {
$this->pass(t('Expected exception has been thrown.'));
}
In fact, the magic is in the node title, which a hook in the test module looks out for, and throws an exception if it spots that title.
There really should be a comment somewhere (either on the title array element, or at the try{} statement?) to say that the implementation of hook_foobar() in the test module will cause the exception to be thrown.
Without it, it's hard to understand how the test works, and also could confuse people into thinking there's something inherently wrong about the code used to create a node -- there isn't.
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | drupal-2244087-10.patch | 484 bytes | cs_shadow |
| #7 | drupal-2244087.patch | 626 bytes | cs_shadow |
| #4 | 2244087-4.patch | 725 bytes | andrei.dincu |
Comments
Comment #1
joachim commented(Background: I needed to create a node programmatically in a test, and figured that the node test cases would be the best source of a simple example on how to do this.)
Comment #2
jhodgdonSounds reasonable...
Comment #3
joachim commentedComment #4
andrei.dincu commentedAdded
// an exception is generated if title is 'testing_transaction_exception'
// see node/tests/modules/node_test_exception/node_test_exception.module
// at hook node_test_exception_node_insert(NodeInterface $node)
as documentation.
Comment #5
jhodgdonThanks! This is the correct documentation to add. We just need the comment to conform to our standards for how comments should be written:
https://drupal.org/node/1354#inline
The main issue is that it should be in complete sentences.
Also, I think it can be a bit shorter -- you don't really need to be so explicit about where to find the function people should look at -- you can just say "See function node_test_exception_node_insert()." in that sentence.
Comment #6
joachim commentedI agree, we don't need to be that detailed.
Even just 'An exception will be thrown by hook node_test_exception_node_insert().' would do. If you want to know more, you can follow the link to that function.
Comment #7
cs_shadow commentedAttaching patch where I've shortened the comment.
Comment #8
jhodgdonThanks - looks good - committed to 8.x. Apparently this needs backporting.
Comment #10
cs_shadow commentedPatch in #7 ported to 7.x
Comment #11
jhodgdonThanks! Looks like the right patch for 7.x.
Comment #12
jhodgdonThanks again! Committed to 7.x.
Comment #15
jhodgdonSilly slow test bot. Of course the patch cannot be applied, it's already applied! :)