You are here

amp_dfp.test in Accelerated Mobile Pages (AMP) 7

Tests for amp_DFP.module.

File

modules/amp_dfp/tests/amp_dfp.test
View source
<?php

/**
 * @file
 * Tests for amp_DFP.module.
 */
class AmpDFPTestCase extends DrupalWebTestCase {
  protected $profile = 'standard';
  protected $admin_user;
  public static function getInfo() {
    return array(
      'name' => 'AMP DFP',
      'description' => 'Tests for the AMP DFP module.',
      'group' => 'AMP',
    );
  }
  protected function setUp() {

    // Enable AMP module.
    parent::setUp('field_ui', 'amp_test', 'dfp', 'amp_dfp');
    theme_enable(array(
      'amptheme',
      'ampsubtheme_example',
    ));

    // Create Admin user.
    $this->admin_user = $this
      ->drupalCreateUser(array(
      'administer content types',
      'administer fields',
      'administer site configuration',
      'administer DFP',
      'administer blocks',
    ));
    $this
      ->drupalLogin($this->admin_user);

    // @todo Install the AMP theme.

    //theme_enable(array('amptheme', 'ampsubtheme_example'));

    //variable_set('amp_theme', 'ampsubtheme_example');

    // Configure dfp.
    variable_set('dfp_network_id', '/1');
  }

  /**
   * Test AMP DFP.
   */
  public function testAmpDFP() {

    // Login as an admin user.
    $this
      ->drupalLogin($this->admin_user);

    // Enable AMP display on article content.
    $this
      ->drupalGet("admin/structure/types/manage/article/display");
    $this
      ->assertResponse(200);
    $edit = [
      "view_modes_custom[amp]" => '1',
    ];
    $this
      ->drupalPost(NULL, $edit, t('Save'));

    // Create DFP tag.
    $this
      ->drupalGet('admin/structure/dfp_ads/add');
    $this
      ->assertResponse(200);
    $edit = array(
      'machinename' => 'amp_ad',
      'slot' => 'AMP Ad',
      'size' => '300x250',
      'targeting[0][target]' => 'position',
      'targeting[0][value]' => '1',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));

    // Create Sticky DFP tag.
    $this
      ->drupalGet('admin/structure/dfp_ads/add');
    $this
      ->assertResponse(200);
    $edit = array(
      'machinename' => 'amp_sticky_ad',
      'slot' => 'AMP Sticky Ad',
      'size' => '320x50',
      'amp_sticky' => '1',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));

    // Blocks placement.
    $this
      ->drupalGet('/admin/structure/block/manage/dfp/amp_ad/configure');
    $this
      ->assertResponse(200);
    $edit = array(
      'regions[ampsubtheme_example]' => 'content',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save block'));
    $this
      ->drupalGet('/admin/structure/block/manage/dfp/amp_sticky_ad/configure');
    $this
      ->assertResponse(200);
    $edit = array(
      'regions[ampsubtheme_example]' => 'content',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save block'));

    // Create a node to test AMP metadata.
    $node = $this
      ->drupalCreateNode(array(
      'type' => 'article',
    ));

    // Check the full display.
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertResponse(200);
    $this
      ->assertNoRaw('<amp-ad');

    // Check the AMP display.
    $this
      ->drupalGet('node/' . $node->nid, array(
      'query' => array(
        'amp' => TRUE,
      ),
    ));
    $this
      ->assertResponse(200);
    $this
      ->assertPattern('|script src="https:\\/\\/cdn.ampproject.org\\/v0\\/amp-ad-0.1.js" async.*\\scustom-element="amp-ad"|');
    $this
      ->assertPattern('|amp-ad type="doubleclick"[\\s].*layout="responsive"[\\s].*height="250"[\\s].*width="300"[\\s].*data-slot="\\/1\\/"[\\s].*json=\'{"targeting":{"position":"1"}}\'|');
    $this
      ->assertPattern('|script src="https:\\/\\/cdn.ampproject.org\\/v0\\/amp-sticky-ad-1.0.js" async.*\\scustom-element="amp-sticky-ad"|');
    $this
      ->assertPattern('|amp-ad type="doubleclick"[\\s].*height="50"[\\s].*width="320"[\\s].*data-slot="\\/1\\/sticky"[\\s]|');
    $this
      ->assertRaw('</amp-ad>');
  }

}

Classes

Namesort descending Description
AmpDFPTestCase @file Tests for amp_DFP.module.