1 node.test NodeBlockFunctionalTest::testRecentNodeBlock()

Tests the recent comments block.


core/modules/node/tests/node.test, line 2345
Tests for node.module.


Functional tests for the node module blocks.


function testRecentNodeBlock() {

  // Disallow anonymous users to view content.
  user_role_change_permissions(BACKDROP_ANONYMOUS_ROLE, array(
    'access content' => FALSE,

  // Set the block to a region to confirm block is available.
  $layout = layout_load('home');
  $block = $layout->addBlock('node', 'recent', 'content');

  // Set block title and variables.
  $block_title = $this->randomName();
  $block->settings['title'] = $block_title;
  $block->settings['title_display'] = LAYOUT_TITLE_CUSTOM;
  $block->settings['block_settings'] = array(
    'node_count' => 2,

  // Test that block is not visible without nodes
  $this->assertText(t('No content available.'), 'Block with "No content available." found.');

  // Add some test nodes.
  $default_settings = array('uid' => $this->web_user->uid, 'type' => 'post');
  $node1 = $this->backdropCreateNode($default_settings);
  $node2 = $this->backdropCreateNode($default_settings);
  $node3 = $this->backdropCreateNode($default_settings);

  // Change the changed time for node so that we can test ordering.
      'changed' => $node1->changed + 100,
    ->condition('nid', $node2->nid)
      'changed' => $node1->changed + 200,
    ->condition('nid', $node3->nid)

  // Test that a user without the 'access content' permission cannot
  // see the block.
  $this->assertNoText($block_title, t('Block was not found.'));

  // Test that only the 2 latest nodes are shown.
  $this->assertNoText($node1->title, t('Node not found in block.'));
  $this->assertText($node2->title, t('Node found in block.'));
  $this->assertText($node3->title, t('Node found in block.'));

  // Check to make sure nodes are in the right order.
  $this->assertTrue($this->xpath('//*[contains(@class,"block-node-recent")]/div/table/tbody/tr[position() = 1]/td/div/a[text() = "' . $node3->title . '"]'), t('Nodes were ordered correctly in block.'));

  // Set the number of recent nodes to show to 10.

  $block->settings['block_settings'] = array(
    'node_count' => 10,

  // Post an additional node.
  $default_settings['type'] = 'page';
  $node4 = $this->backdropCreateNode($default_settings);
  // Clear page caches.

  // Test that all four nodes are shown.
  $this->assertText($node1->title, t('Node found in block.'));
  $this->assertText($node2->title, t('Node found in block.'));
  $this->assertText($node3->title, t('Node found in block.'));
  $this->assertText($node4->title, t('Node found in block.'));

  // Create the custom block.
  $custom_block = array();
  $custom_block['info'] = $this->randomName();
  $custom_block['delta'] = strtolower($this->randomName(8));
  $custom_block['title'] = $this->randomName();
  $custom_block['body[value]'] = $this->randomName(32);
  $this->backdropPost('admin/structure/block/add', $custom_block, t('Save block'));

  // Add a new layout with the node/% path.
  $edit = array(
    'title' => 'Post layout',
    'name' => 'post_layout',
    'layout_template' => 'moscone_flipped',
    'path' => 'node/%',
  $this->backdropPost('admin/structure/layouts/add', $edit, t('Create layout'));

  // Add a condition to specify only the post type should have this layout.
  $this->backdropPost('admin/structure/layouts/manage/post_layout/configure', array(), t('Add visibility condition'));
  $this->backdropPost(NULL, array('condition' => 'node_type'), t('Load condition'));
  $this->backdropPost(NULL, array('bundles[post]' => 1), t('Add visibility condition'));
  $this->backdropPost(NULL, array(), t('Save layout'));

  // Add the custom block to the layout.
  $layout = layout_load('post_layout');
  $layout->addBlock('block', $custom_block['delta'], 'sidebar');

  // Verify visibility rules.
  $this->assertNoText($custom_block['title'], 'Block was not displayed the front page.');
  $this->backdropGet('node/' . $node1->nid);
  $this->assertText($custom_block['title'], 'Block was displayed on the post node.');
  $this->backdropGet('node/' . $node4->nid);
  $this->assertNoText($custom_block['title'], 'Block was not displayed on the page node.');