NodeBlockFunctionalTest.php 4.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

/**
 * @file
 * Definition of Drupal\node\Tests\NodeBlockFunctionalTest.
 */

namespace Drupal\node\Tests;

/**
 * Functional tests for the node module blocks.
 */
class NodeBlockFunctionalTest extends NodeTestBase {
14

15 16 17
  /**
   * An administrative user for testing.
   *
18
   * @var \Drupal\user\UserInterface
19 20 21 22 23 24
   */
  protected $adminUser;

  /**
   * An unprivileged user for testing.
   *
25
   * @var \Drupal\user\UserInterface
26 27 28
   */
  protected $webUser;

29 30 31 32 33 34 35
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('block');

36 37 38 39 40 41 42 43 44
  public static function getInfo() {
    return array(
      'name' => 'Node blocks',
      'description' => 'Test node block functionality.',
      'group' => 'Node',
    );
  }

  function setUp() {
45
    parent::setUp();
46 47

    // Create users and test node.
48 49
    $this->adminUser = $this->drupalCreateUser(array('administer content types', 'administer nodes', 'administer blocks'));
    $this->webUser = $this->drupalCreateUser(array('access content', 'create article content'));
50 51 52
  }

  /**
53
   * Tests the recent comments block.
54
   */
55 56
  public function testRecentNodeBlock() {
    $this->drupalLogin($this->adminUser);
57 58 59 60 61 62

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

63
    // Enable the recent content block with two items.
64
    $block = $this->drupalPlaceBlock('node_recent_block', array('machine_name' => 'test_block', 'block_count' => 2));
65

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

    // Add some test nodes.
71
    $default_settings = array('uid' => $this->webUser->uid, 'type' => 'article');
72 73 74 75 76
    $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.
77
    db_update('node_field_data')
78 79 80 81 82
      ->fields(array(
        'changed' => $node1->changed + 100,
      ))
      ->condition('nid', $node2->nid)
      ->execute();
83
    db_update('node_field_data')
84 85 86 87 88 89 90 91 92 93
      ->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('');
94
    $this->assertNoText($block->label(), 'Block was not found.');
95 96

    // Test that only the 2 latest nodes are shown.
97
    $this->drupalLogin($this->webUser);
98 99 100
    $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.');
101 102

    // Check to make sure nodes are in the right order.
103
    $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.');
104 105

    $this->drupalLogout();
106 107 108
    $this->drupalLogin($this->adminUser);

    // Set the number of recent nodes to show to 10.
109
    $block->getPlugin()->setConfig('block_count', 10);
110
    $block->save();
111 112 113 114 115

    // Post an additional node.
    $node4 = $this->drupalCreateNode($default_settings);
    // drupalCreateNode() does not automatically flush content caches unlike
    // posting a node from a node form.
116
    cache_invalidate_tags(array('content' => TRUE));
117 118 119

    // Test that all four nodes are shown.
    $this->drupalGet('');
120 121 122 123
    $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.');
124

125 126
    // Enable the "Powered by Drupal" block only on article nodes.
    $block = $this->drupalPlaceBlock('system_powered_by_block', array(
127 128 129 130 131 132 133 134 135 136
      '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');
137 138 139

    // Create a page node.
    $node5 = $this->drupalCreateNode(array('uid' => $this->adminUser->uid, 'type' => 'page'));
140 141 142

    // Verify visibility rules.
    $this->drupalGet('');
143 144
    $label = $block->label();
    $this->assertNoText($label, 'Block was not displayed on the front page.');
145
    $this->drupalGet('node/add/article');
146
    $this->assertText($label, 'Block was displayed on the node/add/article page.');
147
    $this->drupalGet('node/' . $node1->nid);
148
    $this->assertText($label, 'Block was displayed on the node/N when node is of type article.');
149
    $this->drupalGet('node/' . $node5->nid);
150
    $this->assertNoText($label, 'Block was not displayed on nodes of type page.');
151
  }
152

153
}