You are here

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

Tests targeting display.

File

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

Class

DisplayTagTest
Tests display of DFP ad tag.

Namespace

Drupal\Tests\dfp\Functional

Code

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

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

  // Create a tag with a target with multiple values.
  $values = [
    $this
      ->randomMachineName(),
    $this
      ->randomMachineName(),
    $this
      ->randomMachineName(),
  ];
  $edit['targeting[0][target]'] = $this
    ->randomMachineName();
  $edit['targeting[0][value]'] = implode(', ', $values);
  $this
    ->dfpEditTag($tag
    ->id(), $edit);
  $this
    ->drupalGet('<front>');
  $this
    ->assertPropertySet('Targeting', $edit['targeting[0][target]'], implode("','", $values));

  // Create a tag with a target but no value.
  $edit['targeting[0][target]'] = $this
    ->randomMachineName();
  $edit['targeting[0][value]'] = '';
  $this
    ->dfpEditTag($tag
    ->id(), $edit);
  $this
    ->assertText(t('The value cannot be empty if a target exists.'));

  // Create a tag with an empty target, but a value.
  $edit['targeting[0][target]'] = '';
  $edit['targeting[0][value]'] = $this
    ->randomMachineName();
  $this
    ->dfpEditTag($tag
    ->id(), $edit);
  $this
    ->assertText(t('The target cannot be empty if a value exists.'));

  // Create a tag with multiple targets.
  $count = 3;
  for ($i = 0; $i < $count; $i++) {
    $edit['targeting[' . $i . '][target]'] = $this
      ->randomMachineName();
    $edit['targeting[' . $i . '][value]'] = $this
      ->randomMachineName();
    $this
      ->dfpEditTag($tag
      ->id(), $edit);
  }
  $this
    ->drupalGet('<front>');
  for ($i = 0; $i < $count; $i++) {
    $this
      ->assertPropertySet('Targeting', $edit['targeting[' . $i . '][target]'], $edit['targeting[' . $i . '][value]']);
  }

  // Test that target can be removed and does not result in empty values.
  $old_target = $edit['targeting[0][target]'];
  $old_value = $edit['targeting[0][value]'];
  $edit['targeting[0][target]'] = '';
  $edit['targeting[0][value]'] = '';
  $this
    ->dfpEditTag($tag
    ->id(), $edit);
  $this
    ->drupalGet('<front>');
  $this
    ->assertPropertyNotSet('Targeting', $old_target, $old_value);
  $this
    ->assertPropertyNotSet('Targeting', '', '');

  // Create a tag that uses the slot token in a target.
  $edit = $this
    ->dfpBasicTagEditValues();
  $test_slot = $this
    ->randomMachineName();
  $edit['slot'] = $test_slot;
  $edit['targeting[0][target]'] = 'slot';
  $edit['targeting[0][value]'] = '[dfp_tag:slot]';
  $this
    ->dfpCreateTag($edit);
  $this
    ->drupalGet('<front>');
  $this
    ->assertPropertySet('Targeting', 'slot', $test_slot);

  // Create a tag that uses the network ID token in a target.
  $edit = $this
    ->dfpBasicTagEditValues();
  $edit['targeting[0][target]'] = 'network id';
  $edit['targeting[0][value]'] = '[dfp_tag:network_id]';
  $this
    ->dfpCreateTag($edit);
  $this
    ->drupalGet('<front>');
  $this
    ->assertPropertySet('Targeting', 'network id', '12345');
}