Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated tests #8

Open
wants to merge 3 commits into
base: 7.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion dfp.info
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ dependencies[] = taxonomy

files[] = plugins/export_ui/dfp_tag_ui.class.inc
files[] = plugins/export_ui/dfp_ctools_export_ui.inc
files[] = tests/dfp.test
files[] = tests/dfp_ad_categories.test
files[] = tests/dfp_ads.test
files[] = tests/dfp_base.test
files[] = tests/dfp_display_tag.test
files[] = tests/dfp_globals.test
files[] = tests/dfp_tokens.test
files[] = tests/dfp_unit.test

testing_api = 2.x
565 changes: 0 additions & 565 deletions tests/dfp.test

This file was deleted.

110 changes: 110 additions & 0 deletions tests/dfp_ad_categories.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/**
* @file
* Ad Categories test file for DFP module.
*/

class dfpAdCategoriesTest extends dfpBaseTest {
/**
* Implements getInfo().
*/
public static function getInfo() {
return array(
'name' => t('DFP Ad Categories'),
'description' => t('Tests the functionality of DFP ad categories.'),
'group' => t('DFP'),
);
}

function testEnableDisableAdCategories() {
// Check that enabling DFP Ad Categories for the "tags" vocabulary works.
$edit = array(
'dfp_enable_ad_categories' => 1,
'dfp_enable_ad_categories_bundles[tags]' => 'tags',
);
$this->drupalPost('admin/structure/dfp_ads/settings', $edit, t('Save configuration'));
$this->drupalGet('admin/structure/taxonomy/tags/add');
$this->assertFieldByName('field_dfp_ad_categories[und]');

// Check that disabling DFP Ad Categories vocabulary works even when a
// bundle is selected.
$edit = array(
'dfp_enable_ad_categories' => 0,
);
$this->drupalPost('admin/structure/dfp_ads/settings', $edit, t('Save configuration'));
$this->drupalGet('admin/structure/taxonomy/tags/add');
$this->assertNoFieldByName('field_dfp_ad_categories[und]');

// Check that disabling DFP Ad Categories on a specific vocabulary works.
// For this test to be valid, we first need to re-enable DFP Ad Categories.
$edit = array(
'dfp_enable_ad_categories' => 1,
'dfp_enable_ad_categories_bundles[tags]' => 'tags',
);
$this->drupalPost('admin/structure/dfp_ads/settings', $edit, t('Save configuration'));
$this->drupalGet('admin/structure/taxonomy/tags/add');
$edit = array(
'dfp_enable_ad_categories_bundles[tags]' => 0,
);
$this->drupalPost('admin/structure/dfp_ads/settings', $edit, t('Save configuration'));
$this->drupalGet('admin/structure/taxonomy/tags/add');
$this->assertNoFieldByName('field_dfp_ad_categories[und]');
}

// Test that DFP Categories are displayed properly.
function testDisplayAdCategories() {
$dfp_add_categories_vocabulary = taxonomy_vocabulary_machine_name_load('dfp_ad_categories');
$tags_vocabulary = taxonomy_vocabulary_machine_name_load('tags');

// Turn on DFP ad categories for the "tags" vocabulary.
$edit = array(
'dfp_enable_ad_categories' => 1,
'dfp_enable_ad_categories_bundles[tags]' => 'tags',
);
$this->drupalPost('admin/structure/dfp_ads/settings', $edit, t('Save configuration'));

// Create a new DFP Ad Category term.
$dfp_category = $this->createTerm($dfp_add_categories_vocabulary);
$control_tag = $this->createTerm($tags_vocabulary);
$test_tag = $this->createTerm($tags_vocabulary);

// Create a new "Article" node with these two tags.
$edit = array(
'title' => $this->randomName(8),
'field_tags[und]' => $control_tag->name . ', ' . $test_tag->name,
);
$this->drupalPost('node/add/article', $edit, t('Save'));

// Edit one the new tags and tag it with a DFP Ad Category.
$edit = array(
'field_dfp_ad_categories[und]' => $dfp_category->tid,
);
$this->drupalPost('taxonomy/term/' . $test_tag->tid . '/edit', $edit, t('Save'));

// Create a DFP ad slot that targets the [dfp_tag:ad_categories] token.
$edit = $this->dfpBasicTagEditValues();

// Create a tag that uses the slot token in a target.
$edit['slot'] = $this->randomName(8);
$edit['targeting[0][target]'] = 'categories';
$edit['targeting[0][value]'] = '[dfp_tag:ad_categories]';
$tag = $this->createTag($edit);

// Check that when we are on the node page, that the DFP Ad Category is used
// and that the control tag is also displayed.
$this->drupalGet('node/1');
$this->assertPropertySet('Targeting', 'categories', $control_tag->name);
$this->assertPropertySet('Targeting', 'categories', $dfp_category->name);

// Check that when we are on the term page for one of DFP Ad Categories that
// it is correctly included.
$this->drupalGet('taxonomy/term/' . $dfp_category->tid);
$this->assertPropertySet('Targeting', 'categories', $dfp_category->name);

// Check that when we are on a term page for a term with a corresponding
// DFP Ad Categories the DFP Ad Category is included.
$this->drupalGet('taxonomy/term/' . $test_tag->tid);
$this->assertPropertySet('Targeting', 'categories', $dfp_category->name);
}
}
42 changes: 42 additions & 0 deletions tests/dfp_ads.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @file
* Ads test file for DFP module.
*/

class dfpAdTestTest extends dfpBaseTest {
/**
* Implements getInfo().
*/
public static function getInfo() {
return array(
'name' => t('Displaying AdTest DFP Tags'),
'description' => t('Tests for displaying overridden DFP tags for ad test purposes.'),
'group' => t('DFP'),
);
}

function testTestPage() {
$tag = $this->createTag();

$override = $this->randomName(8);
$this->drupalGet('admin/structure/dfp_ads/test_page', array('query' => array('adunit_override' => $override)));
$this->assertRaw('googletag.defineSlot("' . variable_get('dfp_network_id', '') . '/' . $override . '"', 'The Ad Unit Name was properly overridden on the Ad Test Page.');
}

function testQueryString() {
$tag = $this->createTag();

$override = $this->randomName(8);
variable_set('dfp_adtest_adunit', $override);
$this->drupalGet('admin/structure/dfp_ads/test_page', array('query' => array('adtest' => 'true')));
$this->assertRaw('googletag.defineSlot("' . variable_get('dfp_network_id', '') . '/' . $override . '"', 'The Ad Unit Name was properly overridden using the adtest query string.');

// Check that nefarious override values are caught because security.
$nefarious_override = "<script>alert('not safe');</script>";
$this->drupalGet('admin/structure/dfp_ads/test_page', array('query' => array('adunit_override' => $nefarious_override)));
$this->assertNoRaw($nefarious_override, 'Only strings without executable code can be used for the adunit_override query string.');
}

}
130 changes: 130 additions & 0 deletions tests/dfp_base.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?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);
}

}
Loading