link.entity_token.test 5.1 KB

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