You are here

public function BlockAttributesAdminConfigTestCase::testAdminConfigForm in Block Attributes 7

Create a block based on default settings and test disabling attributes.

Block Attributes admin configuration form default settings are initialized for all attributes and tested with the creation of a new custom Block. All attributes are then disabled and the Block creation form tested again.

File

./block_attributes.test, line 602
Test the Block Attributes module.

Class

BlockAttributesAdminConfigTestCase
Test Block Attributes admin configuration form.

Code

public function testAdminConfigForm() {

  // A few styles to be randomly selected for the test.
  $styles = array(
    "font-weight: bold;text-decoration: underline;",
    "font-size: 11px; font-weight: normal;",
    "text-align: center;",
  );

  // Initialize random values for the default attributes.
  $block_attributes_defaults = array(
    'block_attributes_accesskey_default' => $this
      ->randomName(1),
    'block_attributes_align_default' => array_rand(array(
      'left' => t('Left'),
      'right' => t('Right'),
      'center' => t('Center'),
      'justify' => t('Justify'),
    )),
    'block_attributes_class_default' => implode(' ', array(
      $this
        ->randomName(8),
      $this
        ->randomName(8),
      $this
        ->randomName(8),
    )),
    'block_attributes_id_default' => $this
      ->randomName(8),
    'block_attributes_style_default' => $styles[mt_rand(0, 2)],
    'block_attributes_title_default' => $this
      ->randomName(8),
  );

  // Update all of Block Attributes default fields.
  $this
    ->drupalPost("admin/structure/block/attributes", $block_attributes_defaults, t('Save configuration'));

  // Check default configuration was saved successfully.
  $this
    ->assertText(t('The configuration options have been saved.'));

  // Get formatted array for all Block attributes with default values.
  $block_attributes = $this
    ->getBlockAttributesTestingData($block_attributes_defaults);

  // Check Block's configuration form HTML attributes field values.
  $this
    ->drupalGet("admin/structure/block/add");
  foreach ($block_attributes as $key => $value) {

    // Check each attribute field has the default value defined previously.
    $this
      ->assertFieldByName($key, $value, format_string('The default value for the field <em>@field_name</em> was found in the custom Block\'s creation page: @field_value', array(
      '@field_name' => $key,
      '@field_value' => $value,
    )));
  }

  // Add a few more field values to the testing dataset.
  $test_attributes = $block_attributes + array(
    'title' => $this
      ->randomName(8),
    'info' => $this
      ->randomName(16),
    'body[value]' => $this
      ->randomName(128),
    'regions[bartik]' => 'content',
  );

  // Update Block's HTML attributes fields and create a new block.
  $this
    ->drupalPost("admin/structure/block/add", $test_attributes, t('Save block'));

  // Check Block configuration was saved successfully.
  $this
    ->assertText(t('The block has been created.'));

  // Browse to the homepage.
  $this
    ->drupalGet('');

  // Assert whether Block's attributes could be found on the page.
  $this
    ->assertBlockAttributesDisplay($test_attributes);

  // Set all the checkboxes to disable all Block attributes.
  $block_attributes_enable = array(
    'block_attributes_accesskey_enable' => FALSE,
    'block_attributes_align_enable' => FALSE,
    'block_attributes_class_enable' => FALSE,
    'block_attributes_id_enable' => FALSE,
    'block_attributes_style_enable' => FALSE,
    'block_attributes_title_enable' => FALSE,
  );

  // Set all Block Attributes to disabled.
  $this
    ->drupalPost("admin/structure/block/attributes", $block_attributes_enable, t('Save configuration'));

  // Check default configuration was saved successfully.
  $this
    ->assertText(t('The configuration options have been saved.'));

  // Check the block creation form and another block's configuration form.
  $paths = array(
    "admin/structure/block/add",
    "admin/structure/block/manage/{$this->module}/{$this->delta}/configure",
  );
  foreach ($paths as $path) {

    // Check that none of the attributes fields display on the forms anymore.
    $this
      ->drupalGet($path);
    foreach ($block_attributes as $key => $value) {
      $this
        ->assertNoFieldByName($key, NULL, format_string('The field <em>@field_name</em> was not found on the page.', array(
        '@field_name' => $key,
      )));
    }

    // Ensure the fieldsets don't display if no attribute is contained.
    // Attempt to select in the HTML markup the three fieldsets with an id
    // starting with 'edit-' and ending with '-attributes'.
    $fieldsets_xpath = $this
      ->xpath('//fieldset[starts-with(@id,"edit-")][substring(@id, string-length(@id) - string-length("-attributes") +1) = "-attributes"][contains(@class, "collapsible")]');
    $this
      ->assertTrue(count($fieldsets_xpath) == 0, 'The block attributes fieldsets could not be found on the page.');
  }
}