link.entity_token.test 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @file
  4. * Contains simpletests making sure entity_token integration works.
  5. */
  6. /**
  7. * Testing that tokens can be used in link titles.
  8. */
  9. class LinkEntityTokenTest extends LinkBaseTestClass {
  10. /**
  11. * Get Info.
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Link entity tokens test',
  16. 'description' => 'Tests that a link field appears properly in entity tokens',
  17. 'group' => 'Link',
  18. 'dependencies' => array('token', 'entity', 'entity_token'),
  19. );
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function setUp(array $modules = array()) {
  25. $modules[] = 'token';
  26. $modules[] = 'entity';
  27. $modules[] = 'entity_token';
  28. parent::setUp($modules);
  29. }
  30. /**
  31. * Creates a link field, fills it, then uses a loaded node to test tokens.
  32. */
  33. public function testFieldTokenNodeLoaded() {
  34. // Create field.
  35. $settings = array(
  36. 'instance[settings][enable_tokens]' => 0,
  37. );
  38. $field_name = $this->createLinkField('page',
  39. $settings);
  40. // Create page form.
  41. $this->drupalGet('node/add/page');
  42. // $field_name = 'field_' . $name;.
  43. $this->assertField($field_name . '[und][0][title]', 'Title found');
  44. $this->assertField($field_name . '[und][0][url]', 'URL found');
  45. $token_url_tests = array(
  46. 1 => array(
  47. 'href' => 'http://example.com/' . $this->randomName(),
  48. 'label' => $this->randomName(),
  49. ),
  50. 2 => array(
  51. 'href' => 'http://example.com/' . $this->randomName() . '?property=value',
  52. 'label' => $this->randomName(),
  53. ),
  54. 3 => array(
  55. 'href' => 'http://example.com/' . $this->randomName() . '#position',
  56. 'label' => $this->randomName(),
  57. ),
  58. 4 => array(
  59. 'href' => 'http://example.com/' . $this->randomName() . '#lower?property=value2',
  60. 'label' => $this->randomName(),
  61. ),
  62. );
  63. // @codingStandardsIgnoreLine
  64. // $this->assert('pass', '<pre>' . print_r($token_url_tests, TRUE) . '<pre>');.
  65. foreach ($token_url_tests as &$input) {
  66. $this->drupalGet('node/add/page');
  67. $edit = array(
  68. 'title' => $input['label'],
  69. $field_name . '[und][0][title]' => $input['label'],
  70. $field_name . '[und][0][url]' => $input['href'],
  71. );
  72. $this->drupalPost(NULL, $edit, t('Save'));
  73. $url = $this->getUrl();
  74. $input['url'] = $url;
  75. }
  76. // Change to anonymous user.
  77. $this->drupalLogout();
  78. foreach ($token_url_tests as $index => $input2) {
  79. $node = node_load($index);
  80. $this->assertNotEqual(NULL, $node, "Do we have a node?");
  81. $this->assertEqual($node->nid, $index, "Test that we have a node.");
  82. $token_name = '[node:' . str_replace('_', '-', $field_name) . ':url]';
  83. $assert_data = token_replace($token_name,
  84. array('node' => $node));
  85. $this->assertEqual($input2['href'], $assert_data, "Test that the url token has been set to " . $input2['href'] . ' - ' . $assert_data);
  86. }
  87. }
  88. /**
  89. * Field Token Node Viewed.
  90. *
  91. * Creates a link field, fills it, then uses a loaded and node_view'd node to
  92. * test tokens.
  93. */
  94. public function testFieldTokenNodeViewed() {
  95. // Create field.
  96. $settings = array(
  97. 'instance[settings][enable_tokens]' => 0,
  98. );
  99. $field_name = $this->createLinkField('page',
  100. $settings);
  101. // Create page form.
  102. $this->drupalGet('node/add/page');
  103. // $field_name = 'field_' . $name;.
  104. $this->assertField($field_name . '[und][0][title]', 'Title found');
  105. $this->assertField($field_name . '[und][0][url]', 'URL found');
  106. $token_url_tests = array(
  107. 1 => array(
  108. 'href' => 'http://example.com/' . $this->randomName(),
  109. 'label' => $this->randomName(),
  110. ),
  111. 2 => array(
  112. 'href' => 'http://example.com/' . $this->randomName() . '?property=value',
  113. 'label' => $this->randomName(),
  114. ),
  115. 3 => array(
  116. 'href' => 'http://example.com/' . $this->randomName() . '#position',
  117. 'label' => $this->randomName(),
  118. ),
  119. 4 => array(
  120. 'href' => 'http://example.com/' . $this->randomName() . '#lower?property=value2',
  121. 'label' => $this->randomName(),
  122. ),
  123. );
  124. //@codingStandardsIgnoreLine
  125. // $this->assert('pass', '<pre>' . print_r($token_url_tests, TRUE) . '<pre>');.
  126. foreach ($token_url_tests as &$input) {
  127. $this->drupalGet('node/add/page');
  128. $edit = array(
  129. 'title' => $input['label'],
  130. $field_name . '[und][0][title]' => $input['label'],
  131. $field_name . '[und][0][url]' => $input['href'],
  132. );
  133. $this->drupalPost(NULL, $edit, t('Save'));
  134. $url = $this->getUrl();
  135. $input['url'] = $url;
  136. }
  137. // Change to anonymous user.
  138. $this->drupalLogout();
  139. foreach ($token_url_tests as $index => $input2) {
  140. $node = node_load($index);
  141. $this->assertNotEqual(NULL, $node, "Do we have a node?");
  142. $this->assertEqual($node->nid, $index, "Test that we have a node.");
  143. $token_name = '[node:' . str_replace('_', '-', $field_name) . ':url]';
  144. $assert_data = token_replace($token_name,
  145. array('node' => $node));
  146. $this->assertEqual($input2['href'], $assert_data, "Test that the url token has been set to " . $input2['href'] . ' - ' . $assert_data);
  147. }
  148. }
  149. }