---
url: 'https://metadrop.net/en/articles/behat3x-steps-of-interactions-with-nodes-for-drupal'
title: 'Behat3.x steps of interactions with Nodes for Drupal'
author: 'Carlos Montes'
date: '2017-01-27T11:19:10+00:00'
updated: '2026-08-04T07:54:06+00:00'
type: article
summary: 'In this article we will add steps to manipulate our Drupal nodes'
tags:
  - Behat
  - Testing
  - 'Drupal 7'
published: true
og:
  determiner: Automatic
  site_name: Metadrop
  'image:alt': 'Behat3.x steps of interactions with Nodes for Drupal'
  street_address: 'Calle Manuel Luna, 12, 3 Dcha'
  locality: Madrid
  region: Madrid
  postal_code: '28020'
  country_name: España
  email: hola@metadrop.net
  phone_number: '910053180'
schema:
  '@context': 'https://schema.org'
  '@graph':
    -
      '@type': Article
      '@id': 'https://metadrop.net/en/articles/behat3x-steps-of-interactions-with-nodes-for-drupal#article'
      name: 'Behat3.x steps of interactions with Nodes for Drupal'
      headline: 'Behat3.x steps of interactions with Nodes for Drupal'
      description: 'In this article we will add steps to manipulate our Drupal nodes'
      about:
        - Behat
        - Testing
        - 'Drupal 7'
      datePublished: '2017-01-13T12:19:10+0100'
      dateModified: '2026-08-04T09:54:06+0200'
      author:
        '@type': Person
        name: 'Carlos Montes'
      publisher:
        '@type': Organization
        '@id': 'https://metadrop.net/#organization'
      mainEntityOfPage: 'https://metadrop.net/en/articles/behat3x-steps-of-interactions-with-nodes-for-drupal'
    -
      '@type': Organization
      '@id': 'https://metadrop.net/#organization'
      url: 'https://metadrop.net/'
      name: Metadrop
      sameAs:
        - 'https://www.drupal.org/metadrop'
        - 'https://twitter.com/metadrop'
        - 'https://asociaciondrupal.es/partner/metadrop'
        - 'https://www.linkedin.com/company/metadrop'
      logo:
        '@type': ImageObject
        url: 'https://metadrop.net/themes/custom/mdrop_radix/logo-metadrop-500-500.jpg'
        width: '500'
        height: '500'
    -
      '@type': ItemPage
      '@id': 'https://metadrop.net/en/articles/behat3x-steps-of-interactions-with-nodes-for-drupal'
      breadcrumb:
        '@type': BreadcrumbList
        itemListElement:
          -
            '@type': ListItem
            position: 1
            name: Home
            item: 'https://metadrop.net/en'
          -
            '@type': ListItem
            position: 2
            name: 'Expert Drupal & Tech Articles'
            item: 'https://metadrop.net/en/articles'
      publisher:
        '@type': Organization
        '@id': 'https://metadrop.net/#organization'
---
 1. [Articles](https://metadrop.net/en/articles)
 
  

# Behat3.x steps of interactions with Nodes for Drupal

Friday, January 13, 2017

 

 



In this article we will add steps to manipulate our Drupal nodes



   



With the help of these two functions we can publish and unpublish nodes. Very useful if we need to have certain content to do tests on them.

## Node Publishing

```php
/**
 * @BeforeSuite
 */
public static
function publishNode() { 
  $nids = array();  // Load all nodes in one go for better performance. Example (12, 15)
   
  $nodes = node_load_multiple($nids);
  foreach($nodes as $node) {
       
    $node-> status = 1;  // set status property to 1, Published
       
    node_save($node);   //save the node
  }
}

```

## Unpublished Nodes

```php
/**
 * @AfterSuite
 */
public static
function unpublishNode() { 
  $nids = array();  // Load all nodes in one go for better performance. Example (1, 5, 15)
   
  $nodes = node_load_multiple($nids); 
  foreach($nodes as $node) {    

    $node-> status = 0; // set status property to 0, Unpublish
       
    node_save($node);   // save the node(s)
  }
}
```

## Retrieve the NID of the last created node

```php
 
 /**
   * Take the last nid create. Optional bundle
   *
   * @param string $bundle
   *   Optionally restrict the search to this type of node.
   *
   * @return int
   *   Node id (Nid).
   */
  public function getLastNodeNid($bundle = NULL) {
    if (!empty($bundle)) {
      $query = db_query("SELECT nid FROM node WHERE type = :bundle ORDER BY nid DESC LIMIT 1", array(':bundle' => $bundle));
    }
    else {
      $query = db_query("SELECT nid FROM node ORDER BY nid DESC LIMIT 1");
    }

    return $query->fetchField();
  }

```

Below is information on the tools and documentation on Behat related to the steps.

Tags Information @BeforeSuite y @AfterSuite
[https://docs.behat.org/en/v2.5/guides/3.hooks.html#hooks](https://docs.behat.org/en/v2.5/guides/3.hooks.html#hooks "Behat documentación hooks")

Information about Drupal Extension and Mink
[https://behat-drupal-extension.readthedocs.io/en/3.1/intro.html](https://behat-drupal-extension.readthedocs.io/en/3.1/intro.html "Behat Drupal Extension")



[Behat](https://metadrop.net/en/articles?text=Behat)

[Testing](https://metadrop.net/en/articles?text=Testing)

[Drupal 7](https://metadrop.net/en/articles?text=Drupal%207)