1 node.test NodeBlockFunctionalTest::testRecentNodeBlock()

Tests the recent content block.

File

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

Class

NodeBlockFunctionalTest
Functional tests for the node module blocks.

Code

function testRecentNodeBlock() {
  $this->backdropLogin($this->admin_user);

  // 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,
  );
  $layout->save();

  // Test that block is not visible without nodes
  $this->backdropGet('');
  $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.
  db_update('node')
    ->fields(array(
      'changed' => $node1->changed + 100,
    ))
    ->condition('nid', $node2->nid)
    ->execute();
  db_update('node')
    ->fields(array(
      'changed' => $node1->changed + 200,
    ))
    ->condition('nid', $node3->nid)
    ->execute();

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

  // Test that only the 2 latest nodes are shown.
  $this->backdropLogin($this->web_user);
  $this->backdropGet('');
  $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.
  $this->backdropLogout();
  $this->backdropLogin($this->admin_user);

  $block->settings['block_settings'] = array(
    'node_count' => 10,
  );
  $layout->save();

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

  // Test that all four nodes are shown.
  $this->backdropGet('');
  $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');
  $layout->save();

  // Verify visibility rules.
  $this->backdropGet('');
  $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.');
}