link.entity_token.test 5.1 KB

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