You are here

public function AmazonNodesTest::testFieldFormatters in Amazon Product Advertisement API 7.2

Test the field formatters.

File

tests/amazon.nodes.test, line 27
Node tests for Amazon module.

Class

AmazonNodesTest
@file Node tests for Amazon module.

Code

public function testFieldFormatters() {

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

  // Add ASIN field to the content type.
  _amazon_test_attach_fields();

  // Check UI
  $this
    ->drupalGet('admin/structure/types/manage/article/fields');
  $this
    ->assertRaw('ASIN');
  $this
    ->assertRaw('field_asin');
  $this
    ->assertLink('Amazon item');
  $this
    ->assertLink('Amazon ASIN Text field');
  $this
    ->drupalGet('admin/structure/types/manage/article/display');
  $this
    ->assertRaw('ASIN');

  // Create a node.
  $amazon_items = amazon_test_get_amazon_items();
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
    'field_asin' => array(
      LANGUAGE_NONE => array(
        array(
          'asin' => $amazon_items[0]['asin'],
        ),
      ),
    ),
  ));
  $test_styles = $this
    ->amazon_test_get_amazon_styles();
  foreach ($test_styles as $style => $details) {

    // Change the field format.
    if ($instance = field_read_instance('node', 'field_asin', 'article')) {
      $instance['display']['default']['type'] = 'asin_' . $style;
      field_update_instance($instance);
    }

    // Test that the markup is correct.
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertResponse(200);
    $this
      ->assertRaw('ASIN:');

    // Test markup.
    $xpath = $details['xpath'];
    foreach ($xpath as $item) {
      $xpath_query = $item['query'];
      $xpath_values = $item['values'];

      // Fill in the value for style.
      if (isset($xpath_values[':style_class'])) {
        $xpath_values[':style_class'] = "amazon-item-{$style}";
      }
      $this
        ->assertElementByXPath($xpath_query, $xpath_values, "Style '{$style}' was rendered correctly.");
    }

    // Test that the urls are not double encoded.
    $this
      ->assertNoRaw('&tag=');
    $this
      ->assertNoRaw('&linkCode=');
    $this
      ->assertNoRaw('&creativeASIN=');
  }
}