static-copy.t 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # Copyright (C) 2007–2014 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software: you can redistribute it and/or modify it
  4. # under the terms of the GNU General Public License as published by the Free
  5. # Software Foundation, either version 3 of the License, or (at your option)
  6. # any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. # more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along with
  14. # this program. If not, see <http://www.gnu.org/licenses/>.
  15. require 't/test.pl';
  16. package OddMuse;
  17. use utf8;
  18. use Test::More tests => 38;
  19. add_module('static-copy.pl');
  20. xpath_test(update_page('Logo', "#FILE image/png\niVBORw0KGgoAAAA",
  21. undef, 0, 1),
  22. '//img[@class="upload"]'
  23. . '[@src="http://localhost/wiki.pl/download/Logo"]'
  24. . '[@alt="Logo"]');
  25. $page = get_page('action=browse id=Logo raw=1');
  26. $page =~ m/^Etag: ([0-9]+)/m;
  27. $ts = $1;
  28. ok($ts > 0, "Got a timestamp for the first revision");
  29. sleep 1;
  30. xpath_test(update_page('HomePage', "[[image:Logo]]"),
  31. '//a[@class="image"]'
  32. . '[@href="http://localhost/wiki.pl/Logo"]'
  33. . '/img[@class="upload"]'
  34. . '[@src="http://localhost/wiki.pl/download/Logo"]'
  35. . '[@alt="Logo"]');
  36. AppendStringToFile($ConfigFile, q{
  37. $StaticAlways = 1;
  38. $StaticDir = $DataDir . '/static';
  39. $StaticUrl = '/static/';
  40. %StaticMimeTypes = ('image/png' => 'png', );
  41. @UploadTypes = ('image/png', );
  42. });
  43. update_page('Logo', "DeletedPage");
  44. xpath_test(update_page('Logo', "#FILE image/png\niVBORw0KGgoAAAA",
  45. undef, 0, 1),
  46. '//img[@class="upload"]'
  47. . '[@src="/static/Logo.png"]'
  48. . '[@alt="Logo"]');
  49. ok(-f "$DataDir/static/Logo.png", "$DataDir/static/Logo.png exists");
  50. xpath_test(update_page('HomePage', "Static: [[image:Logo]]"),
  51. '//a[@class="image"]'
  52. . '[@href="http://localhost/wiki.pl/Logo"]'
  53. . '/img[@class="upload"]'
  54. . '[@src="/static/Logo.png"]'
  55. . '[@alt="Logo"]');
  56. update_page('Logo', "DeletedPage");
  57. xpath_test(get_page("action=rollback id=Logo to=$ts username=root"),
  58. '//h1[text()="Rolling back changes"]',
  59. '//a[@class="local"]'
  60. . '[@href="http://localhost/wiki.pl/Logo"]'
  61. . '[text()="Logo"]'
  62. . '/following-sibling::text()[string()=" rolled back"]');
  63. xpath_test(update_page('HomePage', "Static: [[image:Logo]]"),
  64. '//a[@class="image"]'
  65. . '[@href="http://localhost/wiki.pl/Logo"]'
  66. . '/img[@class="upload"]'
  67. . '[@src="/static/Logo.png"]'
  68. . '[@alt="Logo"]');
  69. # File got restored as well
  70. ok(-f "$DataDir/static/Logo.png", "$DataDir/static/Logo.png exists");
  71. # upload svgz (gzipped)
  72. my $page = <<EOT;
  73. #FILE image/svg+xml gzip
  74. H4sICKlml04AA3Rlc3Quc3ZnADWOQQ6DIBBF956CzF6hdtMY0F1PYA9gCqUkdMbIRKSnLy76t/+/
  75. vK+n4xPF7rYUCA1cOgXC4ZNsQG/gMd/bG4jEC9olEjoDSDCNjU67F5XEZODNvA5S5py7fO1o87JX
  76. Ssm6gEacSVxiJW1Ia1zKEDAGdDDWUrM7WBzVW7XFQK/gv34RcpvC1w29WhnGeSMfyRZ2qOWJ1ROn
  77. Y2x+OvAf9cMAAAA=
  78. EOT
  79. AppendStringToFile($ConfigFile, q{
  80. push(@UploadTypes, "image/svg+xml");
  81. $StaticMimeTypes{"image/svg+xml"} = 'svg';
  82. $StaticMimeTypes{"image/svg+xml gzip"} = 'svgz';
  83. });
  84. # verify upload worked
  85. test_page(update_page('Trogs', $page, undef, 0, 1), # admin
  86. 'contains an uploaded file');
  87. # verify static file exists
  88. ok(-f "$DataDir/static/Trogs.svgz", "$DataDir/static/Trogs.svgz exists");
  89. # verify source link is correct
  90. xpath_test(update_page('HomePage', "Static: [[image:Trogs]]"),
  91. '//a[@class="image"]'
  92. . '[@href="http://localhost/wiki.pl/Trogs"]'
  93. . '/img[@class="upload"]'
  94. . '[@src="/static/Trogs.svgz"]'
  95. . '[@alt="Trogs"]');
  96. # delete the static pages and regenerate it
  97. ok(unlink("$DataDir/static/Trogs.svgz"), "Deleted $DataDir/static/Trogs.svgz");
  98. ok(unlink("$DataDir/static/Logo.png"), "Deleted $DataDir/static/Logo.png");
  99. # StaticWriteFiles must write uploaded files only (since $StaticAlways = 1)
  100. $page = get_page('action=static raw=1 pwd=foo');
  101. test_page($page, "Trogs", "Logo"); # Remember, a rollback has restored Logo.png
  102. test_page_negative($page, "HomePage"); # since it an ordinary page
  103. ok(-s "$DataDir/static/Trogs.svgz", "$DataDir/static/Trogs.svgz has nonzero size");
  104. ok(-s "$DataDir/static/Logo.png", "$DataDir/static/Logo.png has nonzero size");
  105. ok(! -e "$DataDir/static/HomePage.html", "$DataDir/static/HomePage.html does not exist");
  106. # force generation of HomePage using html=1
  107. $page = get_page('action=static raw=1 pwd=foo html=1');
  108. test_page($page, "Trogs", "Logo", "HomePage");
  109. test_page_negative($page, "no data"); # must not skip HomePage!
  110. ok(-s "$DataDir/static/Trogs.svgz", "$DataDir/static/Trogs.svgz has nonzero size");
  111. ok(-s "$DataDir/static/Logo.png", "$DataDir/static/Logo.png has nonzero size");
  112. ok(-s "$DataDir/static/HomePage.html", "$DataDir/static/HomePage.html has nonzero size");
  113. # check that links between pages work as expected
  114. xpath_test(update_page("Test", "Link to HomePage. Testing Ümlaute."),
  115. '//a[text()="HomePage"][@href="http://localhost/wiki.pl/HomePage"]');
  116. test_page(get_page('action=static raw=1 pwd=foo html=1'), 'Test');
  117. xpath_test_file("$DataDir/static/Test.html",
  118. '//a[text()="HomePage"][@href="HomePage.html"]');
  119. test_file("$DataDir/static/Test.html",
  120. "Ümlaute");
  121. SKIP: {
  122. eval {
  123. require LWP::Protocol::https;
  124. };
  125. skip "LWP::Protocol::https not installed", 1 if $@;
  126. test_file("$DataDir/static/static.css",
  127. "This is the default CSS file for Oddmuse wikis");
  128. }
  129. # make sure spaces are translated to underscores (fixed in image.pl)
  130. add_module('image.pl');
  131. # Now, create real pages. First, we'll use the ordinary image link to
  132. # a non-existing page. This should give us an edit link.
  133. xpath_test(update_page('test_image', '[[image:bar baz]]'),
  134. '//a[@class="edit"][@title="Click to edit this page"][@rel="nofollow"][@href="http://localhost/wiki.pl?action=edit;id=bar_baz;upload=1"]');
  135. # The same should be true for the image extension.
  136. xpath_test(update_page('test_image', '[[image/right:bar baz]]'),
  137. '//a[@class="edit"][@title="Click to edit this page"][@rel="nofollow"][@href="http://localhost/wiki.pl?action=edit;id=bar_baz;upload=1"]');
  138. # Next, using a real page. The image type is used appropriately.
  139. xpath_test(update_page('test_image', '[[image/right:Logo]]'),
  140. '//a[@class="image right"][@href="http://localhost/wiki.pl/Logo"]/img[@class="upload"][@src="/static/Logo.png"][@alt="Logo"]');
  141. my $weirdPage = 'Ï_lövé_¥ǫµnĩçȯḑë';
  142. update_page($weirdPage, 'Some text');
  143. update_page('Unicode', '[[' . $weirdPage . ']]');
  144. get_page('action=static raw=1 pwd=foo html=1'); # generate static files
  145. my ($status, $data) = ReadFile("$DataDir/static/Unicode.html");
  146. xpath_test(get_page('Unicode'),
  147. '//a[@class="local"]' . '[@href="http://localhost/wiki.pl/' . UrlEncode($weirdPage) . '"]');
  148. xpath_test($data,
  149. '//a[@class="local"]' . '[@href="' . UrlEncode($weirdPage) . '.html"]');