<?php

/**
 * @file
 * Contains simpletests making sure entity_token integration works.
 */

/**
 * Testing that tokens can be used in link titles.
 */
class LinkEntityTokenTest extends LinkBaseTestClass {

  /**
   * Get Info.
   */
  public static function getInfo() {
    return array(
      'name' => 'Link entity tokens test',
      'description' => 'Tests that a link field appears properly in entity tokens',
      'group' => 'Link',
      'dependencies' => array('token', 'entity', 'entity_token'),
    );
  }

  /**
   * Setup.
   */
  public function setUp($modules = array()) {
    parent::setUp(array('token', 'entity', 'entity_token'));
  }

  /**
   * Creates a link field, fills it, then uses a loaded node to test tokens.
   */
  public function testFieldTokenNodeLoaded() {
    // Create field.
    $settings = array(
      'instance[settings][enable_tokens]' => 0,
    );
    $field_name = $this->createLinkField('page',
      $settings);

    // Create page form.
    $this->drupalGet('node/add/page');
    // $field_name = 'field_' . $name;.
    $this->assertField($field_name . '[und][0][title]', 'Title found');
    $this->assertField($field_name . '[und][0][url]', 'URL found');

    $token_url_tests = array(
      1 => array(
        'href' => 'http://example.com/' . $this->randomName(),
        'label' => $this->randomName(),
      ),
      2 => array(
        'href' => 'http://example.com/' . $this->randomName() . '?property=value',
        'label' => $this->randomName(),
      ),
      3 => array(
        'href' => 'http://example.com/' . $this->randomName() . '#position',
        'label' => $this->randomName(),
      ),
      4 => array(
        'href' => 'http://example.com/' . $this->randomName() . '#lower?property=value2',
        'label' => $this->randomName(),
      ),
    );
    // @codingStandardsIgnoreLine
    // $this->assert('pass', '<pre>' . print_r($token_url_tests, TRUE) . '<pre>');.
    foreach ($token_url_tests as &$input) {
      $this->drupalGet('node/add/page');

      $edit = array(
        'title' => $input['label'],
        $field_name . '[und][0][title]' => $input['label'],
        $field_name . '[und][0][url]' => $input['href'],
      );
      $this->drupalPost(NULL, $edit, t('Save'));
      $url = $this->getUrl();
      $input['url'] = $url;
    }

    // Change to anonymous user.
    $this->drupalLogout();

    foreach ($token_url_tests as $index => $input2) {
      $node = node_load($index);
      $this->assertNotEqual(NULL, $node, "Do we have a node?");
      $this->assertEqual($node->nid, $index, "Test that we have a node.");
      $token_name = '[node:' . str_replace('_', '-', $field_name) . ':url]';
      $assert_data = token_replace($token_name,
        array('node' => $node));
      $this->assertEqual($input2['href'], $assert_data, "Test that the url token has been set to " . $input2['href'] . ' - ' . $assert_data);
    }
  }

  /**
   * Field Token Node Viewed.
   *
   * Creates a link field, fills it, then uses a loaded and node_view'd node to
   * test tokens.
   */
  public function testFieldTokenNodeViewed() {
    // Create field.
    $settings = array(
      'instance[settings][enable_tokens]' => 0,
    );
    $field_name = $this->createLinkField('page',
      $settings);

    // Create page form.
    $this->drupalGet('node/add/page');
    // $field_name = 'field_' . $name;.
    $this->assertField($field_name . '[und][0][title]', 'Title found');
    $this->assertField($field_name . '[und][0][url]', 'URL found');

    $token_url_tests = array(
      1 => array(
        'href' => 'http://example.com/' . $this->randomName(),
        'label' => $this->randomName(),
      ),
      2 => array(
        'href' => 'http://example.com/' . $this->randomName() . '?property=value',
        'label' => $this->randomName(),
      ),
      3 => array(
        'href' => 'http://example.com/' . $this->randomName() . '#position',
        'label' => $this->randomName(),
      ),
      4 => array(
        'href' => 'http://example.com/' . $this->randomName() . '#lower?property=value2',
        'label' => $this->randomName(),
      ),
    );

    //@codingStandardsIgnoreLine
    // $this->assert('pass', '<pre>' . print_r($token_url_tests, TRUE) . '<pre>');.
    foreach ($token_url_tests as &$input) {
      $this->drupalGet('node/add/page');

      $edit = array(
        'title' => $input['label'],
        $field_name . '[und][0][title]' => $input['label'],
        $field_name . '[und][0][url]' => $input['href'],
      );
      $this->drupalPost(NULL, $edit, t('Save'));
      $url = $this->getUrl();
      $input['url'] = $url;
    }

    // Change to anonymous user.
    $this->drupalLogout();

    foreach ($token_url_tests as $index => $input2) {
      $node = node_load($index);
      $this->assertNotEqual(NULL, $node, "Do we have a node?");
      $this->assertEqual($node->nid, $index, "Test that we have a node.");
      $token_name = '[node:' . str_replace('_', '-', $field_name) . ':url]';
      $assert_data = token_replace($token_name,
        array('node' => $node));
      $this->assertEqual($input2['href'], $assert_data, "Test that the url token has been set to " . $input2['href'] . ' - ' . $assert_data);
    }
  }

}
