You are here

dfp_display_tag.test in Doubleclick for Publishers (DFP) 7

Display tag test file for DFP module.

File

tests/dfp_display_tag.test
View source
<?php

/**
 * @file
 * Display tag test file for DFP module.
 */
class dfpDisplayTagTest extends dfpBaseTest {

  /**
   * Implements getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => t('Displaying DFP Tags'),
      'description' => t('Tests for displaying DFP tags.'),
      'group' => t('DFP'),
    );
  }
  function testDisplayTag() {

    // Create a simple tag as a block.
    $tag = $this
      ->createTag();
    $this
      ->assertRaw('googletag.defineSlot("' . variable_get('dfp_network_id', '') . '/' . $tag->adunit . '", ' . dfp_format_size($tag->size) . ', "' . $tag->placeholder_id . '")', 'The ad slot has been correctly defined in javascript.');

    // Create a tag with a machine name longer than 32 characters.
    $edit = array(
      'machinename' => drupal_strtolower($this
        ->randomName(64)),
    );
    $tag = $this
      ->createTag($edit);
    $this
      ->assertRaw('googletag.defineSlot("' . variable_get('dfp_network_id', '') . '/' . $tag->adunit . '", ' . dfp_format_size($tag->size) . ', "' . $tag->placeholder_id . '")', 'The ad slot with a very long machine name has been correctly defined in javascript.');

    // Ensure the DPF click url is safe.
    variable_set('dfp_click_url', 'javascript://example.com?foo&bar');
    $this
      ->drupalGet('/');
    $this
      ->assertNoRaw('javascript://example.com');
    $pattern = '|.setClickUrl\\("http(.*):\\/\\/(.*)javascript%3A\\/\\/example.com%3Ffoo%26bar"\\)|';
    $this
      ->assertPattern($pattern, 'Click URL is filtered for bad protocols and absolute.');
    variable_set('dfp_click_url', 'http://example.com?foo&bar');
    $this
      ->drupalGet('/');
    $this
      ->assertRaw('.setClickUrl("http://example.com?foo&bar")', 'Valid external Click URL is not mangled.');
  }
  function testDisplayTagWithMapping() {
    $edit = $this
      ->dfpBasicTagEditValues();

    // Create a simple tag with a mapping. Verify javascript on page.
    $tag = $this
      ->createTag($edit);
    $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]'] = $this
      ->randomName(8);
    $this
      ->editTag($tag->machinename, $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]'] = $this
      ->randomName(8);
    $this
      ->editTag($tag->machinename, $edit);
    $this
      ->assertText(t('The ad size(s) string can only contain numbers, the character x and commas (unless it is the special keyword "!none").', array(
      '!none' => htmlspecialchars('<none>'),
    )), 'An error was correctly thrown when the Ad Size(s) field contained an invalid pattern.');

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

    // Create a tag without a slug, display it and ensure the default slug is
    // displayed.
    variable_set('dfp_default_slug', '<p>Global slug</p>');
    $edit['settings[slug]'] = '';
    $tag = $this
      ->createTag($edit);
    $this
      ->assertRaw('<div class="slug">' . check_plain(variable_get('dfp_default_slug', '')) . '</div>', 'The default slug is correctly used when no slug exists for an individual tag.');

    // Change the slug to <none> and ensure that no slug is displayed.
    $edit['settings[slug]'] = '<none>';
    $this
      ->editTag($tag->machinename, $edit);
    $this
      ->assertNoRaw('<div class="slug">', t('No slug is appearing when "@none" is used.', array(
      '@none' => '<none>',
    )));

    // Specify a slug and check that it shows instead of the default slug.
    $slug = '<p>Custom slug</p>';
    $edit['settings[slug]'] = $slug;
    $this
      ->editTag($tag->machinename, $edit);
    $this
      ->assertRaw('<div class="slug">' . check_plain($slug) . '</div>', 'The provided slug is correctly displayed with its tag.');
  }
  function testTargeting() {
    $edit = $this
      ->dfpBasicTagEditValues();

    // Create a tag with a target with only one value.
    $tag = $this
      ->createTag($edit);
    $this
      ->assertPropertySet('Targeting', $edit['targeting[0][target]'], $edit['targeting[0][value]']);

    // Create a tag with a target with multiple values.
    $values = array(
      $this
        ->randomName(8),
      $this
        ->randomName(8),
      $this
        ->randomName(8),
    );
    $edit['targeting[0][target]'] = $this
      ->randomName(8);
    $edit['targeting[0][value]'] = implode(', ', $values);
    $this
      ->editTag($tag->machinename, $edit);
    $this
      ->assertPropertySet('Targeting', $edit['targeting[0][target]'], implode('","', $values));

    // Create a tag with a target but no value.
    $edit['targeting[0][target]'] = $this
      ->randomName(8);
    $edit['targeting[0][value]'] = '';
    $this
      ->editTag($tag->machinename, $edit);
    $this
      ->assertText(t('The value cannot be empty if a target exists.'), 'An error was correctly thrown when a target has no value.');

    // Create a tag with an empty target, but a value.
    $edit['targeting[0][target]'] = '';
    $edit['targeting[0][value]'] = $this
      ->randomName(8);
    $this
      ->editTag($tag->machinename, $edit);
    $this
      ->assertText(t('The target cannot be empty if a value exists.'), 'An error was correctly thrown when a target is blank, but a value has been entered.');

    // Create a tag with multiple targets.
    $count = 3;
    for ($i = 0; $i < $count; $i++) {
      $edit['targeting[' . $i . '][target]'] = $this
        ->randomName(8);
      $edit['targeting[' . $i . '][value]'] = $this
        ->randomName(8);
      $this
        ->drupalPost('admin/structure/dfp_ads/list/' . $tag->machinename . '/edit', $edit, t('Add another target'));
      $this
        ->editTag($tag->machinename, $edit);
    }
    for ($i = 0; $i < $count; $i++) {
      $this
        ->assertPropertySet('Targeting', $edit['targeting[' . $i . '][target]'], $edit['targeting[' . $i . '][value]']);
    }

    // Make sure that empty values are ignored.
    $this
      ->drupalPost('admin/structure/dfp_ads/list/' . $tag->machinename . '/edit', $edit, t('Add another target'));
    $this
      ->editTag($tag->machinename, $edit);
    $this
      ->assertPropertyNotSet('Targeting', '', '');

    // Test that target can be removed.
    $edit['targeting[0][target]'] = '';
    $edit['targeting[0][value]'] = '';
    $this
      ->editTag($tag->machinename, $edit);
    $this
      ->assertPropertyNotSet('Targeting', '', '');
  }
  function testBackfill() {
    $edit = $this
      ->dfpBasicTagEditValues();

    // Create a tag with backfill settings.
    $colors = array(
      'background',
      'border',
      'link',
      'text',
      'url',
    );
    $types = array(
      'text',
      'image',
      'text_image',
    );
    shuffle($types);
    $edit['settings[adsense_ad_types]'] = array_pop($types);
    $edit['settings[adsense_channel_ids]'] = $this
      ->randomName(8);
    foreach ($colors as $color) {
      $edit['settings[adsense_colors][' . $color . ']'] = drupal_strtoupper($this
        ->randomName(8));
    }
    $tag = $this
      ->createTag($edit);
    $this
      ->assertPropertySet('', 'adsense_ad_types', $edit['settings[adsense_ad_types]']);
    $this
      ->assertPropertySet('', 'adsense_channel_ids', $edit['settings[adsense_channel_ids]']);
    foreach ($colors as $color) {
      $this
        ->assertPropertySet('', 'adsense_' . $color . '_color', $edit['settings[adsense_colors][' . $color . ']']);
    }
  }

}

Classes

Namesort descending Description
dfpDisplayTagTest @file Display tag test file for DFP module.