search.t 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # Copyright (C) 2006–2018 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. require './t/test.pl';
  16. package OddMuse;
  17. use Test::More tests => 89;
  18. use utf8; # tests contain UTF-8 characters and it matters
  19. add_module('mac.pl');
  20. # Search for broken regular expressions
  21. test_page(get_page('search=%2Btest'), 'Search for: \+test');
  22. # Test search, make sure ordinary users don't see the replacement form
  23. update_page('SearchAndReplace', 'This is fooz and this is barz.', '', 1);
  24. $page = get_page('search=fooz');
  25. test_page($page,
  26. '<h1>Search for: fooz</h1>',
  27. '<p class="result">1 pages found.</p>',
  28. 'This is <strong>fooz</strong> and this is barz.');
  29. xpath_test($page, '//span[@class="result"]/a[@class="local"][@href="http://localhost/wiki.pl/SearchAndReplace"][text()="SearchAndReplace"]');
  30. test_page_negative($page, 'Replace:');
  31. # Search page name
  32. $page = get_page('search=andreplace');
  33. test_page($page,
  34. '<h1>Search for: andreplace</h1>',
  35. '<p class="result">1 pages found.</p>');
  36. # FIXME: Not sure this should work... 'Search<strong>AndReplace</strong>'
  37. xpath_test($page, '//span[@class="result"]/a[@class="local"][@href="http://localhost/wiki.pl/SearchAndReplace"][text()="SearchAndReplace"]');
  38. # Brackets in the page name
  39. test_page(update_page('Search (and replace)', 'Muu'),
  40. 'search=%22Search\+%5c\(and\+replace%5c\)%22');
  41. # Make sure only admins can replace
  42. test_page(get_page('search=foo pwd=foo'),
  43. 'Replace:');
  44. test_page(get_page('search=foo replace=bar'),
  45. 'This operation is restricted to administrators only...');
  46. # Preview simple replacement operation
  47. test_page(get_page('search=fooz replace=fuuz preview=1 pwd=foo'), split('\n',<<'EOT'));
  48. <h1>Preview: fooz &#x2192; fuuz</h1>
  49. <p class="result">1 pages found.</p>
  50. <div class="old"><p>&lt; This is <strong class="changes">fooz</strong> and this is barz.
  51. <div class="new"><p>&gt; This is <strong class="changes">fuuz</strong> and this is barz.
  52. EOT
  53. # Verify that the change has not been made
  54. test_page(get_page('SearchAndReplace'), 'This is fooz and this is barz.');
  55. # Simple replace where the replacement pattern is found
  56. test_page(get_page('search=fooz replace=fuuz pwd=foo'), split('\n',<<'EOT'));
  57. <h1>Replaced: fooz &#x2192; fuuz</h1>
  58. <p class="result">1 pages found.</p>
  59. This is <strong>fuuz</strong> and this is barz.
  60. EOT
  61. # Verify that the change has been made
  62. test_page(get_page('SearchAndReplace'), 'This is fuuz and this is barz.');
  63. # Replace with empty string
  64. test_page(get_page('search=this%20is%20 replace= pwd=foo delete=1'), split('\n',<<'EOT'));
  65. <h1>Replaced: this is &#x2192; </h1>
  66. <p class="result">1 pages found.</p>
  67. fuuz and barz.
  68. EOT
  69. test_page(get_page('SearchAndReplace'), '<p>fuuz and barz.');
  70. # Creating 12 pages
  71. for my $i ('A' .. 'M') {
  72. OpenPage("Page_$i");
  73. Save("Page_$i", 'Something');
  74. }
  75. # Testing default pagination (10 pages)
  76. $page = get_page('search=Something replace=Other preview=1 pwd=foo');
  77. test_page($page, split('\n',<<'EOT'));
  78. <h1>Preview: Something &#x2192; Other</h1>
  79. <p class="result">13 pages found.</p>
  80. <div class="old"><p>&lt; <strong class="changes">Something</strong>
  81. <div class="new"><p>&gt; <strong class="changes">Other</strong>
  82. EOT
  83. test_page($page, map { "Page_$_" } ('A' .. 'J'));
  84. test_page_negative($page, map { "Page_$_" } ('K' .. 'M'));
  85. xpath_test($page, '//a[@class="more"][@href="http://localhost/wiki.pl?search=Something;preview=1;offset=10;num=10;replace=Other"]');
  86. # Next page
  87. $page = get_page('search=Something preview=1 offset=10 num=10 replace=Other pwd=foo');
  88. test_page($page, map { "Page_$_" } ('K' .. 'M'));
  89. # Now do the replacement
  90. $page = get_page('search=Something replace=Other pwd=foo');
  91. test_page($page, 'Replaced: Something &#x2192; Other', '13 pages found',
  92. map { "Page_$_" } ('A' .. 'M'));
  93. # Verify that the change has been made
  94. test_page(get_page('search=Other'), 'Search for: Other', '13 pages found');
  95. # Replace with backreferences, where the replacement pattern is no longer found.
  96. # Take 'fuuz and barz.' and replace ([a-z]+)z with x$1 results in 'xfuu and xbar.'
  97. test_page(get_page('"search=([a-z]%2b)z" replace=x%241 pwd=foo'), '1 pages found');
  98. test_page(get_page('SearchAndReplace'), 'xfuu and xbar.');
  99. # Create an extra page that should not be found
  100. update_page('NegativeSearchTest', 'this page contains an ab');
  101. update_page('NegativeSearchTestTwo', 'this page contains another ab');
  102. test_page(get_page('search=xb replace=[xa]b pwd=foo'), '1 pages found'); # not two ab!
  103. test_page(get_page('SearchAndReplace'), 'xfuu and \[xa\]bar.');
  104. # Handle quoting
  105. test_page(get_page('search=xfuu replace=/fuu/ pwd=foo'), '1 pages found'); # not two ab!
  106. test_page(get_page('SearchAndReplace'), '/fuu/ and \[xa\]bar.');
  107. test_page(get_page('search=/fuu/ replace={{fuu}} pwd=foo'), '1 pages found');
  108. test_page(get_page('SearchAndReplace'), '\{\{fuu\}\} and \[xa\]bar.');
  109. # Check headers especially the quoting of non-ASCII characters.
  110. $page = update_page("Alexander_Schröder", "Edit [[Alexander Schröder]]!");
  111. xpath_test($page,
  112. '//h1/a[@title="Click to search for references to this page"][@href="http://localhost/wiki.pl?search=%22Alexander+Schr%c3%b6der%22"][text()="Alexander Schröder" or text()="' . Encode::encode_utf8('Alexander Schröder') . '"]',
  113. '//a[@class="local"][@href="http://localhost/wiki.pl/Alexander_Schr%c3%b6der"][text()="Alexander Schröder" or text()="' . Encode::encode_utf8('Alexander Schröder') . '"]');
  114. xpath_test(update_page('IncludeSearch',
  115. "first line\n<search \"ab\">\nlast line"),
  116. '//p[text()="first line "]', # note the NL -> SPC
  117. '//div[@class="search"]/p/span[@class="result"]/a[@class="local"][@href="http://localhost/wiki.pl/NegativeSearchTest"][text()="NegativeSearchTest"]',
  118. '//div[@class="search"]/p/span[@class="result"]/a[@class="local"][@href="http://localhost/wiki.pl/NegativeSearchTestTwo"][text()="NegativeSearchTestTwo"]',
  119. '//p[text()=" last line"]'); # note the NL -> SPC
  120. xpath_test(get_page('search=Schröder'),
  121. '//input[@name="search"][@value="Schröder"]');
  122. # Search for zero
  123. update_page("Zero", "This is about 0 and the empty string ''.");
  124. test_page(get_page('search=0'),
  125. '<h1>Search for: 0</h1>',
  126. '<p class="result">1 pages found.</p>',
  127. "This is about <strong>0</strong> and the empty string ''.",
  128. 'meta name="robots" content="NOINDEX,FOLLOW"');
  129. # Search for tags
  130. update_page("Tag", "This is <b>bold</b>.");
  131. test_page(get_page('search="<b>"'),
  132. '<h1>Search for: &lt;b&gt;</h1>',
  133. '<p class="result">1 pages found.</p>',
  134. "This is <strong>&lt;b&gt;</strong>.");
  135. # Search for quoted strings
  136. update_page("Tugend", "Ein wirklich tugendhafter Mensch
  137. bemüht sich nicht um seine Tugend,
  138. darum ist er tugendhaft.");
  139. update_page("Laster", "Ein scheinbar tugendhafter Mensch
  140. bemüht sich dauernd um seine Tugend,
  141. darum ist er nicht wirklich tugendhaft.");
  142. # unordered words
  143. test_page(get_page('search="darum ist er tugendhaft" raw=1'),
  144. 'title: Tugend', 'title: Laster');
  145. # in order
  146. $page = get_page('search="\"darum ist er tugendhaft\"" raw=1');
  147. test_page($page, 'title: Tugend');
  148. test_page_negative($page, 'title: Laster');