drupalextension icon indicating copy to clipboard operation
drupalextension copied to clipboard

DrupalContext::createNode() fails to create nodes for CT's without a moderation workflow enabled

Open hkorik opened this issue 7 years ago • 2 comments

I noticed that only Content Types that have a moderation workflow enabled will the behat tests successfully create nodes. If one chooses a content type that does not have the moderation workflow enabled the test silently fails to create the nodes and it just stops after the step to create the nodes without outputting any failed responses.

After running the behat tests with a debugger I discovered that in the RawDrupalContext class (extended by DrupalContext Class) the nodeCreate method doesn't complete properly in the above instance. The Array that is supposed to be passed back with the createNode method/driver doesn't return anything. See lines below for how the function looks:

 /**
   * Create a node.
   *
   * @return object
   *   The created node.
   */
  public function nodeCreate($node) {
    $this->dispatchHooks('BeforeNodeCreateScope', $node);
    $this->parseEntityFields('node', $node);
    $saved = $this->getDriver()->createNode($node);
    $this->dispatchHooks('AfterNodeCreateScope', $saved);
    $this->nodes[] = $saved;
    return $saved;
  }

I believe the above createNode method eventually calls this following method located at vendor/drupal/drupal-driver/src/Drupal/Driver/Cores/Drupal8.php:

  /**
   * {@inheritdoc}
   */
  public function nodeCreate($node) {
    // Throw an exception if the node type is missing or does not exist.
    if (!isset($node->type) || !$node->type) {
      throw new \Exception("Cannot create content because it is missing the required property 'type'.");
    }
    $bundles = \Drupal::entityManager()->getBundleInfo('node');
    if (!in_array($node->type, array_keys($bundles))) {
      throw new \Exception("Cannot create content because provided content type '$node->type' does not exist.");
    }
    // Default status to 1 if not set.
    if (!isset($node->status)) {
      $node->status = 1;
    }
    // If 'author' is set, remap it to 'uid'.
    if (isset($node->author)) {
      $user = user_load_by_name($node->author);
      if ($user) {
        $node->uid = $user->id();
      }
    }
    $this->expandEntityFields('node', $node);
    $entity = entity_create('node', (array) $node);
    $entity->save();

    $node->nid = $entity->id();

    return $node;
  }

So what I'm finding when the above methods are run on CT's that don't have a moderation workflow enabled when the entity is supposed to be getting saved with "$enity->saved()" in the above function it's not saving the entity and therefor no "nid" is getting captured and returned with the call to that method.

I am seeing the above issue occur with any one of the statements below:

default | Given I am viewing a/an :type (content )with the title :title
default | Given a/an :type (content )with the title :title
default | Given I am viewing my :type (content )with the title :title
default | Given :type content:
default | Given I am viewing a/an :type( content):
default | Then I should be able to edit a/an :type( content)

Some example statements and the output when selecting the above mentioned content types:

Scenario Line 29:

  @api
  Scenario: Create many nodes
    Given "page" content:
      | title    |
      | Page one |
      | Page two |
    And "article" content:
      | title          |
      | First article  |
      | Second article |
    And I am logged in as a user with the "administrator" role
    When I go to "admin/content"
    Then I should see "Page one"
    And I should see "Page two"
    And I should see "First article"
    And I should see "Second article"

Output:

➜  build git:(GIT-00--testing-behat-setup) ✗ behat features/drupal.feature:29
@api
Feature: Content Management
  When I log into the website
  As an administrator
  I should be able to create, edit, and delete page content

  @api
  Scenario: Create many nodes                                  # features/drupal.feature:29
➜  build git:(GIT-00--testing-behat-setup) ✗ 

Scenario Line 21:

  @api
  Scenario: Create node
    Given I am viewing an "article" content:
      | title    | My title   |
    And I am logged in as a user with the "administrator" role
    When I go to "admin/content"
    Then I should see "First"

Output:

➜  build git:(GIT-00--testing-behat-setup) ✗ behat features/drupal.feature:21
@api
Feature: Content Management
  When I log into the website
  As an administrator
  I should be able to create, edit, and delete page content

  @api
  Scenario: Create nodes                                       # features/drupal.feature:21
➜  build git:(GIT-00--testing-behat-setup) ✗ 

Scenario Line 8:

  @api
  Scenario: Create a node
    Given I am logged in as a user with the "administrator" role
    When I am viewing a page with the title "My article"
    Then I should see the heading "My article"

Output:

➜  build git:(GIT-00--testing-behat-setup) ✗ behat features/drupal.feature:8 
@api
Feature: Content Management
  When I log into the website
  As an administrator
  I should be able to create, edit, and delete page content

  @api
  Scenario: Create a node                                        # features/drupal.feature:8
    Given I am logged in as a user with the "administrator" role # Drupal\DrupalExtension\Context\DrupalContext::assertAuthenticatedByRole()
➜  build git:(GIT-00--testing-behat-setup) ✗ 

Scenario Line 14

  @api
  Scenario: Create node
    Given I am viewing a "course" with the title "My article"
    And I am logged in as a user with the "administrator" role
    When I go to "admin/content"
    Then I should see "My article"

Output:

➜  build git:(GIT-00--testing-behat-setup) ✗ behat features/drupal.feature:14
@api
Feature: Content Management
  When I log into the website
  As an administrator
  I should be able to create, edit, and delete page content

  @api
  Scenario: Create node                                        # features/drupal.feature:14
➜  build git:(GIT-00--testing-behat-setup) ✗ 

hkorik avatar Jul 17 '17 03:07 hkorik

@jhedstrom I discovered the exact instance where this is an issue. In a case where a content type is configured without a moderation workflow in place that is when the above failure to create the nodes is occurring - when I write a behat test choosing a content type that has the moderation workflow set the behat test runs smoothly.

Content Type with Moderation workflow enabled: screen shot 2017-07-17 at 5 09 10 am

I wasn't able to get to the root cause of this if it's something that is being caused from Core code or from the DrupalExtension code but one thing is for certain I am consistently seeing the above issue only with content types that don't have a moderation workflow in place. I will update the original issue and title to reflect as such.

If the cause to this has something to do with the code provided by DrupalExtension perhaps if we can adjust the logic to work for any given content type whether it has a moderation workflow in place or not? If there is a specific reason why it's set as above if you would be so kind as to explain why that is the case.

hkorik avatar Jul 17 '17 09:07 hkorik

Is this still an issue with the latest content moderation in core? IIRC, I've seen non-moderated content get created on recent projects...

jhedstrom avatar Mar 22 '18 21:03 jhedstrom