2

I have a method which calls another one to retrieve a specific node ID. If it can be found, it then retrieves the node.

Is this the kind of functionality that can be tested in Drupal? Or even should it tested? If so what is the best approach for it?

private function retrieve() {
  // Call another method to get IDs of specific bundle based on its workflow status. We get just one ID back as there is only one node for this bundle.

  // If statement to validate we do have a node ID.
  // Call method to retrieve content for node based on the retrieved `nid`
  // Return node content if any has been found

  // If no content is found return FALSE.
}
  • At a glance you would create some dummy nodes in your setup() method in the test in various workflow states, then test your implementation code based on those to see you get the results that you expect. – Kevin Oct 29 '19 at 16:06
  • Actually after re-reading your code and kiamlaluno's answer, I am not sure why this specifically would need tests, unless you have custom code and you want to test that code. – Kevin Oct 29 '19 at 20:46
  • @Kevin I probably wasn't clear enough on that, basically once I've gotten the content I process it and return only part of it in a different state to how it came out of Drupal and that's the bit I'm trying to test. – Neil Nand Oct 29 '19 at 21:51
1

By definition, when unit testing your function you should focus on the logic the unit delivers, which, in your case, is the following conditional logic:

IF (nid IS NOT FOUND) OR (a node for a found nid HAS NO CONTENT)
  => FALSE
ELSE
  => NODE.CONTENT

That being said, you could:

  1. Mock the two internally-used methods:
    • methodA:
      • CaseA1: returns an empty result (no nid found)
      • CaseA2: returns X as a nid
      • CaseA3: returns Y as a nid
    • methodB:
      • CaseB1: Given X as a parameter returns an empty result (no content found)
      • CaseB2: Given Y as a parameter returns some dummy content
  2. Assert that the retrieve() method returns for each of the following cases:
    • CaseA1: FALSE
    • CaseA2+CaseB1: FALSE
    • CaseA3+CaseB2: TRUE

This is not very "deep" testing, but your function does not carry much logic. It is still testable in this minimal way.

Good luck!

0

Yes, it can be tested, but if the purpose is just checking if a module you are developing is able to find a node created locally, that is quite what the NodeLoadMultipleTest class does.

  public function testNodeMultipleLoad() {
    $node1 = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
    $node2 = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
    $node3 = $this->drupalCreateNode(['type' => 'article', 'promote' => 0]);
    $node4 = $this->drupalCreateNode(['type' => 'page', 'promote' => 0]);

    // Confirm that promoted nodes appear in the default node listing.
    $this->drupalGet('node');
    $this->assertText($node1->label(), 'Node title appears on the default listing.');
    $this->assertText($node2->label(), 'Node title appears on the default listing.');
    $this->assertNoText($node3->label(), 'Node title does not appear in the default listing.');
    $this->assertNoText($node4->label(), 'Node title does not appear in the default listing.');

    // Load nodes with only a condition. Nodes 3 and 4 will be loaded.
    $nodes = $this->container->get('entity_type.manager')
      ->getStorage('node')
      ->loadByProperties(['promote' => 0]);
    $this->assertEqual($node3->label(), $nodes[$node3->id()]->label(), 'Node was loaded.');
    $this->assertEqual($node4->label(), $nodes[$node4->id()]->label(), 'Node was loaded.');
    $count = count($nodes);
    $this->assertTrue($count == 2, new FormattableMarkup('@count nodes loaded.', [
      '@count' => $count,
    ]));
    // Omissis.
  }

if the purpose is not testing a functionality very specific of the module, for example retrieving a node created on a remote site, I would not write a test for verifying the module is able to retrieve a node.
Notice that also the Workflows and the Content Moderation modules (included in Drupal core) have their own tests. Chances are there is already a test for the functionality you want to test, if you want to load nodes with specific workflow states.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.