block.test 7.31 KB
Newer Older
1 2 3 4
<?php
// $Id$

class BlockTestCase extends DrupalWebTestCase {
5 6
  protected $regions;

7 8 9 10 11 12 13 14 15 16 17 18
  function getInfo() {
    return array(
      'name' => t('Block functionality'),
      'description' => t('Add, edit and delete custom block. Configure and move a module-defined block.'),
      'group' => t('Block'),
    );
  }

  function setUp() {
    parent::setUp();

    // Create and login user
19
    $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer filters'));
20
    $this->drupalLogin($admin_user);
21

22
    // Define the existing regions
23 24 25 26 27 28
    $this->regions = array();
    $this->regions[] = array('name' => 'header', 'id' => 'header-region');
    $this->regions[] = array('name' => 'left', 'id' => 'sidebar-left');
    $this->regions[] = array('name' => 'content', 'id' => 'center');
    $this->regions[] = array('name' => 'right', 'id' => 'sidebar-right');
    $this->regions[] = array('name' => 'footer');
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  }

  /**
   * Test creating custom block (i.e. box), moving it to a specific region and then deleting it.
   */
  function testBox() {
    // Add a new box by filling out the input form on the admin/build/block/add page.
    $box = array();
    $box['info'] = $this->randomName(8);
    $box['title'] = $this->randomName(8);
    $box['body'] = $this->randomName(32);
    $this->drupalPost('admin/build/block/add', $box, t('Save block'));

    // Confirm that the box has been created, and then query the created bid.
    $this->assertText(t('The block has been created.'), t('Box successfully created.'));
44
    $bid = db_result(db_query("SELECT bid FROM {box} WHERE info = '%s'", array($box['info'])));
45 46 47 48

    // Check to see if the box was created by checking that it's in the database..
    $this->assertNotNull($bid, t('Box found in database'));

49 50 51 52 53 54
    // Check if the block can be moved to all availble regions.
    $box['module'] = 'block';
    $box['delta'] = $bid;
    foreach ($this->regions as $region) {
      $this->moveBlockToRegion($box, $region);
    }
55 56

    // Delete the created box & verify that it's been deleted and no longer appearing on the page.
57
    $this->drupalPost('admin/build/block/delete/' . $bid, array(), t('Delete'));
58 59 60 61
    $this->assertRaw(t('The block %title has been removed.', array('%title' => $box['info'])), t('Box successfully deleted.'));
    $this->assertNoText(t($box['title']), t('Box no longer appears on page.'));
  }

62 63 64 65 66 67 68 69 70 71 72 73 74
  /**
   * Test creating custom block (i.e. box) using Full HTML.
   */
  function testBoxFormat() {
    // Add a new box by filling out the input form on the admin/build/block/add page.
    $box = array();
    $box['info'] = $this->randomName(8);
    $box['title'] = $this->randomName(8);
    $box['body'] = '<h1>Full HTML</h1>';
    $box['body_format'] = 2;
    $this->drupalPost('admin/build/block/add', $box, t('Save block'));

    // Set the created box to a specific region.
75
    $bid = db_result(db_query("SELECT bid FROM {box} WHERE info = '%s'", array($box['info'])));
76 77 78 79
    $edit = array();
    $edit['block_' . $bid . '[region]'] = 'left';
    $this->drupalPost('admin/build/block', $edit, t('Save blocks'));

80
    // Confirm that the box is being displayed using configured text format.
81 82 83
    $this->assertRaw('<h1>Full HTML</h1>', t('Box successfully being displayed using Full HTML.'));
  }

84 85 86 87 88 89 90 91 92 93 94
  /**
   * Test configuring and moving a module-define block to specific regions.
   */
  function testBlock() {
    // Select the Navigation block to be configured and moved.
    $block = array();
    $block['module'] = 'user';
    $block['delta'] = 'navigation';
    $block['title'] = $this->randomName(8);

    // Set block title to confirm that interface works and override any custom titles.
95
    $this->drupalPost('admin/build/block/configure/' . $block['module'] . '/' . $block['delta'], array('title' => $block['title']), t('Save block'));
96
    $this->assertText(t('The block configuration has been saved.'), t('Block title set.'));
97
    $bid = db_result(db_query("SELECT bid FROM {block} WHERE module = '%s' AND delta = %d", array($block['module'], $block['delta'])));
98 99 100 101

    // Check to see if the block was created by checking that it's in the database.
    $this->assertNotNull($bid, t('Block found in database'));

102 103 104 105
    // Check if the block can be moved to all availble regions.
    foreach ($this->regions as $region) {
      $this->moveBlockToRegion($block, $region);
    }
106 107 108

    // Set the block to the disabled region.
    $edit = array();
109
    $edit[$block['module'] . '_' . $block['delta'] . '[region]'] = '-1';
110 111 112 113 114 115
    $this->drupalPost('admin/build/block', $edit, t('Save blocks'));

    // Confirm that the block was moved to the proper region.
    $this->assertText(t('The block settings have been updated.'), t('Block successfully move to disabled region.'));
    $this->assertNoText(t($block['title']), t('Block no longer appears on page.'));

116 117 118 119
    // Confirm that the regions xpath is not availble
    $xpath = '//div[@id="block-block-' . $bid . '"]/*';
    $this->assertNoFieldByXPath($xpath, FALSE, t('Box found in no regions. '));

120 121
    // For convenience of developers, put the navigation block back.
    $edit = array();
122
    $edit[$block['module'] . '_' . $block['delta'] . '[region]'] = 'left';
123
    $this->drupalPost('admin/build/block', $edit, t('Save blocks'));
124
    $this->assertText(t('The block settings have been updated.'), t('Block successfully move to left region.'));
125

126
    $this->drupalPost('admin/build/block/configure/' . $block['module'] . '/' . $block['delta'], array('title' => 'Navigation'), t('Save block'));
127 128
    $this->assertText(t('The block configuration has been saved.'), t('Block title set.'));
  }
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  function moveBlockToRegion($block, $region) {
    // If an id for an region hasn't been specified, we assume it's the same as the name.
    if (!(isset($region['id']))) {
      $region['id'] = $region['name'];
    }

    // Set the created block to a specific region.
    $edit = array();
    $edit[$block['module'] . '_' . $block['delta'] . '[region]'] = $region['name'];
    $this->drupalPost('admin/build/block', $edit, t('Save blocks'));

    // Confirm that the block was moved to the proper region.
    $this->assertText(t('The block settings have been updated.'), t('Block successfully moved to %region_name region.', array( '%region_name'=> $region['name'])));

    // Confirm that the block is being displayed.
    $this->assertText(t($block['title']), t('Block successfully being displayed on the page.'));

    // Confirm that the box was found at the proper region.
    $xpath = '//div[@id="' . $region['id'] . '"]//div[@id="block-' . $block['module'] . '-' . $block['delta'] . '"]/*';
    $this->assertFieldByXPath($xpath, FALSE, t('Box found in %region_name region.', array('%region_name' => $region['name'])));
  }
151 152 153 154 155 156 157 158 159 160 161 162 163 164
}

class NonDefaultBlockAdmin extends DrupalWebTestCase {
  function getInfo() {
    return array(
      'name' => t('Non default theme admin'),
      'description' => t('Check the administer page for non default theme.'),
      'group' => t('Block'),
    );
  }

  /**
   * Test non-default theme admin.
   */
165
  function testNonDefaultBlockAdmin() {
166 167 168 169 170 171
    $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer site configuration'));
    $this->drupalLogin($admin_user);
    $this->drupalPost('admin/build/themes', array('status[bluemarine]' => 1), t('Save configuration'));
    $this->drupalGet('admin/build/block/list/bluemarine');
    $this->assertRaw('bluemarine/style.css', t('Bluemarine CSS found'));
  }
172
}