You are here

public function TestSubContext::assertValidImageRegion in Panopoly 8.2

Same name and namespace in other branches
  1. 7 modules/panopoly/panopoly_test/behat/steps/panopoly_test.behat.inc \TestSubContext::assertValidImageRegion()

Asserts that an image is present and not broken.

@Then I should see an image in the :region region

File

modules/panopoly/panopoly_test/behat/steps/panopoly_test.behat.inc, line 828
Provide Behat step-definitions for generic Panopoly tests.

Class

TestSubContext
Behat sub-context for Panopoly.

Code

public function assertValidImageRegion($region) {
  $regionObj = $this
    ->getRegion($region);
  $elements = $regionObj
    ->findAll('css', 'img');
  if (empty($elements)) {
    throw new \Exception(sprintf('No image was not found in the "%s" region on the page %s', $region, $this
      ->getSession()
      ->getCurrentUrl()));
  }
  if ($src = $elements[0]
    ->getAttribute('src')) {
    $params = [
      'http' => [
        'method' => 'HEAD',
      ],
    ];
    $context = stream_context_create($params);
    $fp = @fopen($src, 'rb', FALSE, $context);
    if (!$fp) {
      throw new \Exception(sprintf('Unable to download <img src="%s"> in the "%s" region on the page %s', $src, $region, $this
        ->getSession()
        ->getCurrentUrl()));
    }
    $meta = stream_get_meta_data($fp);
    fclose($fp);
    if ($meta === FALSE) {
      throw new \Exception(sprintf('Error reading from <img src="%s"> in the "%s" region on the page %s', $src, $region, $this
        ->getSession()
        ->getCurrentUrl()));
    }
    $wrapper_data = $meta['wrapper_data'];
    $found = FALSE;
    if (is_array($wrapper_data)) {
      foreach ($wrapper_data as $header) {
        if (substr(strtolower($header), 0, 19) == 'content-type: image') {
          $found = TRUE;
        }
      }
    }
    if (!$found) {
      throw new \Exception(sprintf('Not a valid image <img src="%s"> in the "%s" region on the page %s', $src, $region, $this
        ->getSession()
        ->getCurrentUrl()));
    }
  }
  else {
    throw new \Exception(sprintf('No image had no src="..." attribute in the "%s" region on the page %s', $region, $this
      ->getSession()
      ->getCurrentUrl()));
  }
}