context.test 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. class CtoolsContextKeywordsSubstitutionTestCase extends DrupalWebTestCase {
  3. public static function getInfo() {
  4. return array(
  5. 'name' => 'Keywords substitution',
  6. 'description' => 'Verify that keywords are properly replaced with data.',
  7. 'group' => 'ctools',
  8. );
  9. }
  10. public function setUp() {
  11. parent::setUp('ctools');
  12. ctools_include('context');
  13. }
  14. public function testKeywordsSubstitution() {
  15. // Create node context for substitution.
  16. $node = $this->drupalCreateNode();
  17. $context = ctools_context_create('node', $node);
  18. $contexts = array('argument_1' => $context);
  19. // Run tests on some edge cases.
  20. $checks = array(
  21. '%node:changed:raw:' => array(
  22. "{$node->changed}:",
  23. t('Multi-level token has been replaced. Colon left untouched.'),
  24. ),
  25. '%node:title' => array(
  26. "{$node->title}",
  27. t('Keyword and converter have been replaced.'),
  28. ),
  29. '%%node:title' => array(
  30. "%node:title",
  31. t('Keyword after escaped percent sign left untouched.'),
  32. ),
  33. '%node:title%node:nid' => array(
  34. "{$node->title}{$node->nid}",
  35. t('Multiple substitutions have been replaced.'),
  36. ),
  37. '%node:title:' => array(
  38. "{$node->title}:",
  39. t('Colon after keyword and converter left untouched.'),
  40. ),
  41. '%node:title%%' => array(
  42. "{$node->title}%",
  43. t('Escaped percent sign after keyword and converter left untouched.'),
  44. ),
  45. '%%%node:title' => array(
  46. "%{$node->title}",
  47. t('Keyword after escaped and unescaped percent sign has been replaced.'),
  48. ),
  49. '%%foo:bar' => array(
  50. "%foo:bar",
  51. t('Non-existant context ignored.'),
  52. ),
  53. 'There was about 20%-30% difference in price.' => array(
  54. 'There was about 20%-30% difference in price.',
  55. t('Non-keyword percent sign left untouched.'),
  56. ),
  57. 'href="my%20file%2dname.pdf"' => array(
  58. 'href="my%20file%2dname.pdf"',
  59. t('HTTP URL escape left untouched.'),
  60. ),
  61. 'href="my%a0file%fdname.pdf"' => array(
  62. 'href="my%a0file%fdname.pdf"',
  63. t('HTTP URL escape (high-chars) left untouched.'),
  64. ),
  65. '<a href="http://www.example.com/here%20is%20a%20pdf.pdf">Click here!</a>' => array(
  66. '<a href="http://www.example.com/here%20is%20a%20pdf.pdf">Click here!</a>',
  67. t('HTTP URL escape percent sign left untouched in HTML.'),
  68. ),
  69. 'SELECT * FROM {table} WHERE field = "%s"' => array(
  70. 'SELECT * FROM {table} WHERE field = "%s"',
  71. t('SQL percent sign left untouched.'),
  72. ),
  73. );
  74. foreach ($checks as $string => $expectations) {
  75. list($expected_result, $message) = $expectations;
  76. $actual_result = ctools_context_keyword_substitute($string, array(), $contexts);
  77. $this->assertEqual($actual_result, $expected_result, $message);
  78. }
  79. }
  80. }