Newer
Older
<?php
/**
* @file
* Definition of Drupal\node\Tests\NodeBlockFunctionalTest.
*/
namespace Drupal\node\Tests;
/**
* Functional tests for the node module blocks.
*/
class NodeBlockFunctionalTest extends NodeTestBase {
Dries Buytaert
committed
/**
* An administrative user for testing.
*
* @var \Drupal\user\Plugin\Core\Entity\User
*/
protected $adminUser;
/**
* An unprivileged user for testing.
*
* @var \Drupal\user\Plugin\Core\Entity\User
*/
protected $webUser;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('block');
public static function getInfo() {
return array(
'name' => 'Node blocks',
'description' => 'Test node block functionality.',
'group' => 'Node',
);
}
function setUp() {
parent::setUp();
// Create users and test node.
Dries Buytaert
committed
$this->adminUser = $this->drupalCreateUser(array('administer content types', 'administer nodes', 'administer blocks'));
$this->webUser = $this->drupalCreateUser(array('access content', 'create article content'));
Jennifer Hodgdon
committed
* Tests the recent comments block.
Dries Buytaert
committed
public function testRecentNodeBlock() {
$this->drupalLogin($this->adminUser);
// Disallow anonymous users to view content.
user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
'access content' => FALSE,
));
Angie Byron
committed
// Enable the recent content block with two items.
$block = $this->drupalPlaceBlock('node_recent_block', array('machine_name' => 'test_block', 'block_count' => 2));
Dries Buytaert
committed
// Test that block is not visible without nodes.
$this->drupalGet('');
Jennifer Hodgdon
committed
$this->assertText(t('No content available.'), 'Block with "No content available." found.');
// Add some test nodes.
Dries Buytaert
committed
$default_settings = array('uid' => $this->webUser->uid, 'type' => 'article');
$node1 = $this->drupalCreateNode($default_settings);
$node2 = $this->drupalCreateNode($default_settings);
$node3 = $this->drupalCreateNode($default_settings);
// Change the changed time for node so that we can test ordering.
db_update('node_field_data')
->fields(array(
'changed' => $node1->changed + 100,
))
->condition('nid', $node2->nid)
->execute();
db_update('node_field_data')
->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->drupalLogout();
$this->drupalGet('');
Angie Byron
committed
$this->assertNoText($block->label(), 'Block was not found.');
// Test that only the 2 latest nodes are shown.
Dries Buytaert
committed
$this->drupalLogin($this->webUser);
Jennifer Hodgdon
committed
$this->assertNoText($node1->label(), 'Node not found in block.');
$this->assertText($node2->label(), 'Node found in block.');
$this->assertText($node3->label(), 'Node found in block.');
// Check to make sure nodes are in the right order.
Angie Byron
committed
$this->assertTrue($this->xpath('//div[@id="block-test-block"]/div/table/tbody/tr[position() = 1]/td/div/a[text() = "' . $node3->label() . '"]'), 'Nodes were ordered correctly in block.');
$this->drupalLogout();
Dries Buytaert
committed
$this->drupalLogin($this->adminUser);
// Set the number of recent nodes to show to 10.
$block->getPlugin()->setConfig('block_count', 10);
Angie Byron
committed
$block->save();
// Post an additional node.
$node4 = $this->drupalCreateNode($default_settings);
// drupalCreateNode() does not automatically flush content caches unlike
// posting a node from a node form.
cache_invalidate_tags(array('content' => TRUE));
// Test that all four nodes are shown.
$this->drupalGet('');
Jennifer Hodgdon
committed
$this->assertText($node1->label(), 'Node found in block.');
$this->assertText($node2->label(), 'Node found in block.');
$this->assertText($node3->label(), 'Node found in block.');
$this->assertText($node4->label(), 'Node found in block.');
Angie Byron
committed
// Enable the "Powered by Drupal" block only on article nodes.
$block = $this->drupalPlaceBlock('system_powered_by_block', array(
Angie Byron
committed
'visibility' => array(
'node_type' => array(
'types' => array(
'article' => 'article',
),
),
),
));
$visibility = $block->get('visibility');
$this->assertTrue(isset($visibility['node_type']['types']['article']), 'Visibility settings were saved to configuration');
Dries Buytaert
committed
// Create a page node.
$node5 = $this->drupalCreateNode(array('uid' => $this->adminUser->uid, 'type' => 'page'));
// Verify visibility rules.
$this->drupalGet('');
Angie Byron
committed
$label = $block->label();
$this->assertNoText($label, 'Block was not displayed on the front page.');
$this->drupalGet('node/add/article');
Angie Byron
committed
$this->assertText($label, 'Block was displayed on the node/add/article page.');
$this->drupalGet('node/' . $node1->nid);
Angie Byron
committed
$this->assertText($label, 'Block was displayed on the node/N when node is of type article.');
Dries Buytaert
committed
$this->drupalGet('node/' . $node5->nid);
Angie Byron
committed
$this->assertNoText($label, 'Block was not displayed on nodes of type page.');
Angie Byron
committed