1 layout.test LayoutBlockTest::testHeroBlocks()

Test Hero blocks.

File

core/modules/layout/tests/layout.test, line 1830
Tests for the Layout module.

Class

LayoutBlockTest
Tests the blocks title, content, and display settings.

Code

function testHeroBlocks() {
  $hero_title_1 = 'Wonderwoman';
  $hero_content_1 = 'Youth is your greatest weapon, your greatest tool.';
  $hero_title_2 = 'Black Widow';
  $hero_content_2 = "This isnt gonna end well...";

  // Create some sample images.
  $good_image = imagecreate(2400, 400);
  $good_filename = 'good.jpg';
  $good_realpath_filename = backdrop_realpath('temporary://' . $good_filename);
  imagejpeg($good_image, $good_realpath_filename);

  $bad_image = imagecreate(1024, 260);
  imagecolorallocate($bad_image, 255, 0, 0); // Red image.
  $bad_filename = 'bad.jpg';
  $bad_realpath_filename = backdrop_realpath('temporary://' . $bad_filename);
  imagejpeg($bad_image, $bad_realpath_filename);

  $good_image2 = imagecreate(2400, 400);
  $good_filename2 = 'good2.jpg';
  $good_realpath_filename2 = backdrop_realpath('temporary://' . $good_filename2);
  imagejpeg($good_image2, $good_realpath_filename2);

  $this->backdropGet('admin/structure/layouts/manage/default');

  // Block 1: Add a hero block with no image to the content area.
  $this->clickLink(t('Add block'), 2);
  $this->clickLink(t('Hero block'));
  $edit = array(
    'title' => $hero_title_1,
    'content[value]' => $hero_content_1,
  );
  $this->backdropPost(NULL, $edit, t('Add block'));

  // Block 2: Add a hero block with only content and an image.
  $this->clickLink(t('Add block'), 2);
  $this->clickLink(t('Hero block'));
  $edit = array(
    'content[value]' => $hero_content_2,
    'files[image]' => $good_realpath_filename,
  );
  $this->backdropPost(NULL, $edit, t('Add block'));

  // Block 3: Add a hero block with only an image.
  $this->clickLink(t('Add block'), 2);
  $this->clickLink(t('Hero block'));

  // Confirm a too-small background image fails validation.
  $edit = array(
    'files[image]' => $bad_realpath_filename,
  );
  $this->backdropPost(NULL, $edit, t('Add block'));
  $error_message = t('The specified file %name could not be uploaded.', array('%name' => $bad_filename));
  $error_message .= ' ' . t('The image is too small; the minimum dimensions are %dimensions pixels.', array('%dimensions' => '1200x300'));
  $this->assertRaw($error_message);

  // Resubmit using the good image.
  $edit['files[image]'] = $good_realpath_filename2;
  $this->backdropPost(NULL, $edit, t('Add block'));

  // Save the layout.
  $this->backdropPost(NULL, array(), t('Save layout'));

  // Go to the user page and check for all hero blocks.
  $this->backdropGet('user');

  // Get all hero blocks on the user page and confirm the correct number.
  $elements = $this->xpath('//*[contains(@class,:region)]//*[contains(@class,:block)]', array(
    ':region' => 'l-content',
    ':block' => 'block-layout-hero',
  ));
  $this->assertEqual(count($elements), 3, 'Three hero blocks were found in the content area.');

  // Get all the hero block elements for testing. Order matters here.
  $elements = $this->xpath('(//div[contains(@class, "block-layout-hero")])');

  // Block 1: Confirm both title and content are visible.
  $this->assertText($hero_title_1);
  $this->assertText($hero_content_1);
  // Block 1: Confirm handy `block-hero-no-image` class is present.
  $classes_1 = (string) $elements[0]['class'];
  $this->assertTrue(strstr($classes_1, 'block-hero-no-image'));
  // Block 1: Confirm that no background image is present.
  $this->assertNull($elements[0]['style'], 'No image present for 1st hero block.');

  // Block 2: no title present.
  $title_2 = $elements[1]->xpath('.//h2');
  $this->assertIdentical($title_2, array(), 'No title present for 2nd hero block.');
  // Block 2: content is visible.
  $this->assertText($hero_content_2);
  // Block 2: Confirm handy `block-hero-image` class is present.
  $classes_2 = (string) $elements[1]['class'];
  $this->assertTrue(strstr($classes_2, 'block-hero-image'));
  // Block 2: Background image present.
  $style_2 = (string) $elements[1]['style'];
  $this->assertTrue(strstr($style_2, $good_filename), 'Image present for 2nd hero block.');

  // Block 3: no title present.
  $title_3 = $elements[2]->xpath('.//h2');
  $this->assertIdentical($title_3, array(), 'No title present for 3rd hero block.');
  // Block 3: no content present.
  $content_3_element = $elements[2]->xpath('.//div[contains(@class, "block-content")]');
  $this->assertEqual(count($content_3_element), 1, '3rd hero block content found.');
  // Extract the content.
  $content_3 = (string) $content_3_element[0];

  // We inserted an HTML non-breaking space, but SimpleXML will return a
  // unicode non-breaking space (\u00a0). PHP <7.0.0 does not have native
  // unicode strings, so we encode as JSON which will encode the character.
  // In PHP 7 this could be represented as $content_3 === "\u00a0".
  $unicode_nbsp = trim(json_encode(trim($content_3)), '"');
  $this->assertEqual($unicode_nbsp, '\u00a0', '3rd hero block contains only a non-breaking space.');

  // Background image present for block three.
  $style_3 = (string) $elements[2]['style'];
  $this->assertTrue(strstr($style_3, $good_filename2), 'Image present for 3rd hero block.');
}