You are here

context_blocks.test in Context 6.2

File

tests/context_blocks.test
View source
<?php

class ContextBlocksTestCase extends DrupalWebTestCase {

  /**
   * Implementation of get_info() for information
   */
  function getInfo() {
    return array(
      'name' => t('Context block reactions'),
      'description' => t('Checks that our implementation of the block reactions works.'),
      'group' => t('Context'),
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    parent::setUp('context');

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

  /**
   * Check that the block title setting is respected by Context for custom blocks.
   */
  function testContextCustomBlockTitles() {

    // 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.'));
    $bid = db_result(db_query("SELECT bid FROM {boxes} WHERE info = '%s'", array(
      $box['info'],
    )));

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

    // Confirm that the box is NOT being displayed.
    $this
      ->assertNoText(t($box['title']), t('Box successfully being NOT displayed on the page.'));

    // Add the block via context:
    $context = array(
      'namespace' => $this
        ->randomName(8),
      'attribute' => $this
        ->randomName(8),
      'value' => $this
        ->randomName(8),
      'sitewide' => '1',
      'block' => array(
        $box['module'] . '_' . $box['delta'] => array(
          'module' => $box['module'],
          'delta' => $box['delta'],
          'region' => 'left',
        ),
      ),
    );
    context_save_context((object) $context);

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

    // Change the title of the block:
    $box['old_title'] = $box['title'];
    $box['title'] = '<none>';

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

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

    // Confirm that the block is NOT being displayed with its old title.
    $this
      ->assertNoText(t($box['old_title']), t('Old box title successfully being NOT displayed on the page.'));
  }

  /**
   * Check that the block title setting is respected by Context.
   */
  function testContextBlockTitles() {

    // Select the Navigation block to be configured and moved.
    $block = array();
    $block['module'] = 'user';
    $block['delta'] = 1;
    $block['title'] = $this
      ->randomName(8);

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

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

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

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

    // Add the block via context:
    $context = array(
      'namespace' => $this
        ->randomName(8),
      'attribute' => $this
        ->randomName(8),
      'value' => $this
        ->randomName(8),
      'sitewide' => '1',
      'block' => array(
        $block['module'] . '_' . $block['delta'] => array(
          'module' => $block['module'],
          'delta' => $block['delta'],
          'region' => 'left',
        ),
      ),
    );
    context_save_context((object) $context);

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

    // Change the title of the block:
    $block['old_title'] = $block['title'];
    $block['title'] = '<none>';

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

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

    // Confirm that the block is NOT being displayed with its old title.
    $this
      ->assertNoText(t($block['old_title']), t('Old block title successfully being NOT displayed on the page.'));
    $account = $this->loggedInUser;
    $this
      ->assertNoText(t($account->name), t('Block tile successfully being NOT displayed on the page.'));

    // Change the title of the block to the default:
    $block['title'] = '';

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

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

    // Confirm that the block is NOT being displayed with its old title.
    $this
      ->assertNoText(t($block['old_title']), t('Old block title successfully being NOT displayed on the page.'));
    $account = $this->loggedInUser;
    $this
      ->assertText(t($account->name), t('Default block tile successfully being displayed on the page.'));
  }

}

Classes