page_tokens.test 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @file
  4. * Tests for different parts of the ctools object caching system.
  5. *
  6. * Needs to be a WebTest because need access to database tables.
  7. */
  8. class CtoolsPageTokens extends DrupalWebTestCase {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. public static function getInfo() {
  13. return array(
  14. 'name' => 'Page token processing',
  15. 'description' => 'Verify that page tokens can be set and read.',
  16. 'group' => 'ctools',
  17. 'dependencies' => array('ctools'),
  18. );
  19. }
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function setUp(array $modules = array()) {
  24. $modules[] = 'ctools';
  25. parent::setUp($modules);
  26. // Start each test anew.
  27. ctools_reset_page_tokens();
  28. }
  29. /**
  30. * Test that we can set page tokens.
  31. */
  32. public function testSetPageToken() {
  33. ctools_set_page_token('id', 'variable', 'test');
  34. $tokens = ctools_set_page_token();
  35. $this->assertTrue(is_array($tokens) && count($tokens) === 1, 'Page tokens no longer empty.');
  36. $this->assertEqual($tokens['id'], array('variable', 'test'), 'Page token has correct value.');
  37. }
  38. /**
  39. * Test that we can set page tokens.
  40. */
  41. public function testSetVariableToken() {
  42. $string = ctools_set_variable_token('title');
  43. $tokens = ctools_set_page_token();
  44. $this->assertTrue(is_array($tokens) && count($tokens) === 1, 'Page tokens no longer empty');
  45. $this->assertEqual($tokens[$string], array('variable', 'title'), 'Page tokens no longer empty');
  46. }
  47. /**
  48. * Test that we can set page tokens.
  49. */
  50. public function testReplaceVariableToken() {
  51. $string = ctools_set_variable_token('title');
  52. $this->assertEqual($string, '<!-- ctools-page-title -->', 'Expected form of token was found');
  53. $elements = array(
  54. '#type' => 'container',
  55. '#attributes' => array('class' => array('test')),
  56. 'title' => '<h2>Title</h2>',
  57. 'content' => array(
  58. '#type' => 'markup',
  59. '#markup' => t('This is my test markup'),
  60. '#prefix' => $string,
  61. ),
  62. 'link' => array(
  63. '#type' => 'link',
  64. '#title' => t('My link'),
  65. '#href' => 'node/1',
  66. ),
  67. );
  68. $markup = '<div class="test"><!-- ctools-page-title -->This is my test markup<a href="node/1">My link</a></div>';
  69. $new_markup = ctools_page_token_processing($markup, $elements);
  70. $this->assertTrue(is_string($new_markup) && strlen($new_markup) > 1, 'Should return string');
  71. $this->assertTrue(strstr($new_markup, '<h2>'), 'Variable Token Markup should contain h2 element');
  72. $this->assertFalse(strstr($new_markup, 'ctools-page-title'), 'Variable Token Markup should not contain comment element');
  73. }
  74. /**
  75. * Test that we can set page tokens.
  76. */
  77. public function testReplaceCallbackToken() {
  78. $string = ctools_set_callback_token('title', 'test_ctools_page_callback_token');
  79. $this->assertEqual($string, '<!-- ctools-page-title -->', 'Expected form of token was found');
  80. $elements = array(
  81. '#type' => 'container',
  82. '#attributes' => array('class' => array('test')),
  83. 'content' => array(
  84. '#type' => 'markup',
  85. '#markup' => t('This is my test markup'),
  86. '#prefix' => $string,
  87. ),
  88. 'link' => array(
  89. '#type' => 'link',
  90. '#title' => t('My link'),
  91. '#href' => 'node/1',
  92. ),
  93. );
  94. $markup = '<div class="test"><!-- ctools-page-title -->This is my test markup<a href="node/1">My link</a></div>';
  95. $new_markup = ctools_page_token_processing($markup, $elements);
  96. $this->assertTrue(is_string($new_markup) && strlen($new_markup) > 1, 'Should return a non-empty string');
  97. $this->assertTrue(strstr($new_markup, '<h2>'), 'Callback Token Markup should contain h2 element');
  98. $this->assertFalse(strstr($new_markup, 'ctools-page-title'), 'Callback Token Markup should not contain comment element');
  99. }
  100. }
  101. /**
  102. *
  103. * @param $elements
  104. *
  105. * @return string
  106. */
  107. function test_ctools_page_callback_token($elements) {
  108. // Check that 'elements' array looks good.
  109. if (isset($elements['content'])) {
  110. return '<h2>Title</h2>';
  111. }
  112. else {
  113. return '<!--bad-->';
  114. }
  115. }