You are here

public function DisplayTagTest::testDisplayTagWithMapping in Doubleclick for Publishers (DFP) 8

Tests breakpoint mappings.

@todo add multiple breakpoint configs to the tag.

File

tests/src/Functional/DisplayTagTest.php, line 44

Class

DisplayTagTest
Tests display of DFP ad tag.

Namespace

Drupal\Tests\dfp\Functional

Code

public function testDisplayTagWithMapping() {
  $edit = $this
    ->dfpBasicTagEditValues();

  // Create a simple tag with a mapping. Verify javascript on page.
  $tag = $this
    ->dfpCreateTag($edit);
  $this
    ->drupalGet('<front>');
  $mapping_sizes = explode(',', $edit['breakpoints[0][ad_sizes]']);
  $size_count = count($mapping_sizes);

  // Calculate addSize mappings.
  $mapping_tag = '.addSize(';
  $mapping_tag .= '[' . str_replace('x', ', ', $edit['breakpoints[0][browser_size]']) . '], ';
  $mapping_tag .= $size_count > 1 ? '[' : '';
  for ($i = 0; $i < $size_count; $i++) {
    $mapping_sizes[$i] = '[' . str_replace('x', ', ', $mapping_sizes[$i]) . ']';
    $mapping_tag .= $i + 1 !== $size_count ? $mapping_sizes[$i] . ', ' : $mapping_sizes[$i];
  }
  $mapping_tag .= $size_count > 1 ? '])' : ')';
  $this
    ->assertRaw('googletag.sizeMapping()', 'The ad slot correctly attaches size mapping.');
  $this
    ->assertRaw('.defineSizeMapping(mapping)', 'The ad slot correctly defines size mapping.');
  $this
    ->assertRaw($mapping_tag, 'The ad slot correctly defines specific size mappings.');

  // Create a tag with invalid browser size mappings.
  $edit['breakpoints[0][browser_size]'] = '100y100';
  $this
    ->dfpEditTag($tag
    ->id(), $edit);
  $this
    ->assertText(t('The browser size can only contain numbers and the character x.'), 'An error was correctly thrown when invalid characters.');

  // Create a tag with invalid ad size mappings.
  $edit['breakpoints[0][browser_size]'] = $this
    ->dfpGenerateSize();
  $edit['breakpoints[0][ad_sizes]'] = '100y100,200x200';
  $this
    ->dfpEditTag($tag
    ->id(), $edit);
  $this
    ->assertText(t('The ad size(s) string can only contain numbers, the character x and commas (unless it is the special keyword "&lt;none&gt;").'), 'An error was correctly thrown when invalid characters.');

  // Test tags with ad_size set to <none>.
  $edit['breakpoints[0][browser_size]'] = '0x0';
  $edit['breakpoints[0][ad_sizes]'] = '<none>';
  $this
    ->dfpEditTag($tag
    ->id(), $edit);
  $this
    ->drupalGet('');
  $this
    ->assertRaw('addSize([0, 0], [])');
}