You are here

dfp_base.test in Doubleclick for Publishers (DFP) 7

Base test file for DFP module.

File

tests/dfp_base.test
View source
<?php

/**
 * @file
 * Base test file for DFP module.
 */
class dfpBaseTest extends DrupalWebTestCase {

  /**
   * Implementation of setUp().
   */
  function setUp() {

    // Enable a couple modules.
    parent::setUp('ctools', 'dfp', 'taxonomy');
    menu_rebuild();

    // Create an admin user with all the permissions needed to run tests.
    $this->admin_user = $this
      ->drupalCreateUser(array(
      'administer DFP',
      'administer taxonomy',
      'access administration pages',
      'administer blocks',
      'bypass node access',
      'administer nodes',
      'edit terms in 1',
    ));
    $this
      ->drupalLogin($this->admin_user);

    // Add the some global settings needed for dfp ads to work.
    variable_set('dfp_network_id', '12345');
    variable_set('dfp_default_slug', $this
      ->randomName(8));
  }

  /**
   * Create a simple $edit array to be used on the DFP tag form to create a new
   * DFP tag object.
   */
  function dfpBasicTagEditValues() {
    $machinename = drupal_strtolower($this
      ->randomName(8));
    $basic_tag = array(
      'machinename' => $machinename,
      'slot' => $machinename,
      'size' => implode(',', $this
        ->dfpGenerateSize(2)),
      'adunit' => $this
        ->randomName(8),
      'block' => 1,
      'settings[slug]' => $this
        ->randomName(32),
      'settings[adsense_ad_types]' => '',
      'settings[adsense_channel_ids]' => '',
      'settings[adsense_colors][background]' => '',
      'settings[adsense_colors][border]' => '',
      'settings[adsense_colors][link]' => '',
      'settings[adsense_colors][text]' => '',
      'settings[adsense_colors][url]' => '',
      'targeting[0][target]' => $this
        ->randomName(8),
      'targeting[0][value]' => $this
        ->randomName(8),
      'breakpoints[0][browser_size]' => $this
        ->dfpGenerateSize(),
      'breakpoints[0][ad_sizes]' => implode(',', $this
        ->dfpGenerateSize(2)),
    );
    return $basic_tag;
  }

  /**
   * Generate a random size (or array or sizes) to use when testing tags
   * formatted as ###x###.
   */
  function dfpGenerateSize($count = 1) {
    $sizes = array(
      '300x250',
      '300x600',
      '728x90',
      '728x10',
      '160x600',
      '120x80',
      '300x100',
      '50x50',
      '160x300',
    );
    shuffle($sizes);
    return $count == 1 ? array_pop($sizes) : array_slice($sizes, 0, min($count, count($sizes)));
  }

  /**
   * Creates a basic dfp ad tag.
   */
  function createTag($edit = array()) {

    // Create a new tag.
    $edit += $this
      ->dfpBasicTagEditValues();
    $this
      ->drupalPost('admin/structure/dfp_ads/add', $edit, t('Save'));

    // Load the tag object.
    $tag = dfp_tag_load($edit['machinename']);
    $this
      ->assertTrue(is_object($tag) && ($tag->machinename = $edit['machinename']), 'The new tag was saved correctly.');

    // Display the new tag.
    $edit = array();
    $delta = 'dfp_' . (drupal_strlen($tag->machinename) >= 32 ? md5($tag->machinename) : $tag->machinename);
    $edit['blocks[' . $delta . '][region]'] = 'sidebar_first';
    $this
      ->drupalPost('admin/structure/block', $edit, t('Save blocks'));
    return $tag;
  }

  /**
   * Edit a given tag specified by $machinename with the given values.
   */
  function editTag($machinename, &$edit) {

    // Make sure there is no machinename set when we are editing.
    if (isset($edit['machinename'])) {
      unset($edit['machinename']);
    }
    $this
      ->drupalPost('admin/structure/dfp_ads/list/' . $machinename . '/edit', $edit, t('Save'));
    $tag = dfp_tag_load($machinename);
    return $tag;
  }

  /**
   * Create a taxonomy term.
   */
  function createTerm($vocabulary) {
    $term = new stdClass();
    $term->name = $this
      ->randomName();
    $term->description = $this
      ->randomName();

    // Use the first available text format.
    $term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)
      ->fetchField();
    $term->vid = $vocabulary->vid;
    taxonomy_term_save($term);
    return $term;
  }

  /**
   * Assert that a property is properly being set.
   */
  function assertPropertySet($property, $key, $val) {
    $pattern = '|' . '.set' . $property . '\\("' . $key . '",{1}\\s(.)*' . addslashes($val) . '|';
    $this
      ->assertPattern($pattern, 'A ' . $property . ' property was set for ' . $key . ' = ' . $val);
  }

  /**
   * Assert that a property is not being set.
   */
  function assertPropertyNotSet($property, $key, $val) {
    $pattern = '|' . '.set' . $property . '\\("' . $key . '",{1}\\s(.)*' . addslashes($val) . '|';
    $this
      ->assertNoPattern($pattern, 'A ' . $property . ' property was not set for ' . $key . ' = ' . $val);
  }

}

Classes

Namesort descending Description
dfpBaseTest @file Base test file for DFP module.