summaryrefslogtreecommitdiffstats
path: root/test/functional/wiki_controller_test.rb
blob: 193efb0b7f23232c1b7fa055796ee0dd70b542aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
# encoding: utf-8
#
# Redmine - project management software
# Copyright (C) 2006-2017  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

require File.expand_path('../../test_helper', __FILE__)

class WikiControllerTest < Redmine::ControllerTest
  fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles,
           :enabled_modules, :wikis, :wiki_pages, :wiki_contents,
           :wiki_content_versions, :attachments,
           :issues, :issue_statuses, :trackers

  def setup
    User.current = nil
  end

  def test_show_start_page
    get :show, :params => {:project_id => 'ecookbook'}
    assert_response :success

    assert_select 'h1', :text => /CookBook documentation/
    # child_pages macro
    assert_select 'ul.pages-hierarchy>li>a[href=?]', '/projects/ecookbook/wiki/Page_with_an_inline_image',
      :text => 'Page with an inline image'
  end

  def test_export_link
    Role.anonymous.add_permission! :export_wiki_pages
    get :show, :params => {:project_id => 'ecookbook'}
    assert_response :success
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation.txt'
  end

  def test_show_page_with_name
    get :show, :params => {:project_id => 1, :id => 'Another_page'}
    assert_response :success

    assert_select 'h1', :text => /Another page/
    # Included page with an inline image
    assert_select 'p', :text => /This is an inline image/
    assert_select 'img[src=?][alt=?]', '/attachments/download/3/logo.gif', 'This is a logo'
  end

  def test_show_old_version
    with_settings :default_language => 'en' do
      get :show, :params => {:project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '2'}
    end
    assert_response :success

    assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/1', :text => /Previous/
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2/diff', :text => /diff/
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/3', :text => /Next/
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation', :text => /Current version/
  end

  def test_show_old_version_with_attachments
    page = WikiPage.find(4)
    assert page.attachments.any?
    content = page.content
    content.text = "update"
    content.save!

    get :show, :params => {:project_id => 'ecookbook', :id => page.title, :version => '1'}
    assert_response :success
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/Page_with_an_inline_image', :text => /Current version/
  end

  def test_show_old_version_without_permission_should_be_denied
    Role.anonymous.remove_permission! :view_wiki_edits

    get :show, :params => {:project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '2'}
    assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fprojects%2Fecookbook%2Fwiki%2FCookBook_documentation%2F2'
  end

  def test_show_first_version
    with_settings :default_language => 'en' do
      get :show, :params => {:project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '1'}
    end
    assert_response :success

    assert_select 'a', :text => /Previous/, :count => 0
    assert_select 'a', :text => /diff/, :count => 0
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => /Next/
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation', :text => /Current version/
  end

  def test_show_redirected_page
    WikiRedirect.create!(:wiki_id => 1, :title => 'Old_title', :redirects_to => 'Another_page')

    get :show, :params => {:project_id => 'ecookbook', :id => 'Old_title'}
    assert_redirected_to '/projects/ecookbook/wiki/Another_page'
  end

  def test_show_with_sidebar
    page = Project.find(1).wiki.pages.new(:title => 'Sidebar')
    page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
    page.save!

    get :show, :params => {:project_id => 1, :id => 'Another_page'}
    assert_response :success
    assert_select 'div#sidebar', :text => /Side bar content for test_show_with_sidebar/
  end

  def test_show_should_display_section_edit_links
    @request.session[:user_id] = 2
    get :show, :params => {:project_id => 1, :id => 'Page with sections'}

    assert_select 'a[href=?]', '/projects/ecookbook/wiki/Page_with_sections/edit?section=1', 0
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/Page_with_sections/edit?section=3'
  end

  def test_show_current_version_should_display_section_edit_links
    @request.session[:user_id] = 2
    get :show, :params => {:project_id => 1, :id => 'Page with sections', :version => 3}

    assert_select 'a[href=?]', '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
  end

  def test_show_old_version_should_not_display_section_edit_links
    @request.session[:user_id] = 2
    get :show, :params => {:project_id => 1, :id => 'Page with sections', :version => 2}

    assert_select 'a[href=?]', '/projects/ecookbook/wiki/Page_with_sections/edit?section=2', 0
  end

  def test_show_unexistent_page_without_edit_right
    get :show, :params => {:project_id => 1, :id => 'Unexistent page'}
    assert_response 404
  end

  def test_show_unexistent_page_with_edit_right
    @request.session[:user_id] = 2
    get :show, :params => {:project_id => 1, :id => 'Unexistent page'}
    assert_response :success
    assert_select 'textarea[name=?]', 'content[text]'
  end

  def test_show_specific_version_of_an_unexistent_page_without_edit_right
    get :show, :params => {:project_id => 1, :id => 'Unexistent page', :version => 1}
    assert_response 404
  end

  def test_show_unexistent_page_with_parent_should_preselect_parent
    @request.session[:user_id] = 2
    get :show, :params => {:project_id => 1, :id => 'Unexistent page', :parent => 'Another_page'}
    assert_response :success
    assert_select 'select[name=?] option[value="2"][selected=selected]', 'wiki_page[parent_id]'
  end

  def test_show_should_not_show_history_without_permission
    Role.anonymous.remove_permission! :view_wiki_edits
    get :show, :params => {:project_id => 1, :id => 'Page with sections', :version => 2}

    assert_response 302
  end

  def test_show_page_without_content_should_display_the_edit_form
    @request.session[:user_id] = 2
    WikiPage.create!(:title => 'NoContent', :wiki => Project.find(1).wiki)

    get :show, :params => {:project_id => 1, :id => 'NoContent'}
    assert_response :success
    assert_select 'textarea[name=?]', 'content[text]'
  end

  def test_get_new
    @request.session[:user_id] = 2

    get :new, :params => {:project_id => 'ecookbook'}
    assert_response :success
    assert_select 'input[name=?]', 'title'
  end

  def test_get_new_xhr
    @request.session[:user_id] = 2

    get :new, :params => {:project_id => 'ecookbook'}, :xhr => true
    assert_response :success
    assert_include 'Unallowed characters', response.body
  end

  def test_post_new_with_valid_title_should_redirect_to_edit
    @request.session[:user_id] = 2

    post :new, :params => {:project_id => 'ecookbook', :title => 'New Page'}
    assert_redirected_to '/projects/ecookbook/wiki/New_Page'
  end

  def test_post_new_xhr_with_valid_title_should_redirect_to_edit
    @request.session[:user_id] = 2

    post :new, :params => {:project_id => 'ecookbook', :title => 'New Page'}, :xhr => true
    assert_response :success
    assert_equal 'window.location = "/projects/ecookbook/wiki/New_Page"', response.body
  end

  def test_post_new_should_redirect_to_edit_with_parent
    @request.session[:user_id] = 2

    post :new, :params => {:project_id => 'ecookbook', :title => 'New_Page', :parent => 'Child_1'}
    assert_redirected_to '/projects/ecookbook/wiki/New_Page?parent=Child_1'
  end

  def test_post_new_with_invalid_title_should_display_errors
    @request.session[:user_id] = 2

    post :new, :params => {:project_id => 'ecookbook', :title => 'Another page'}
    assert_response :success
    assert_select_error 'Title has already been taken'
  end

  def test_post_new_with_protected_title_should_display_errors
    Role.find(1).remove_permission!(:protect_wiki_pages)
    @request.session[:user_id] = 2

    post :new, :params => {:project_id => 'ecookbook', :title => 'Sidebar'}
    assert_response :success
    assert_select_error /Title/
  end

  def test_post_new_xhr_with_invalid_title_should_display_errors
    @request.session[:user_id] = 2

    post :new, :params => {:project_id => 'ecookbook', :title => 'Another page'}, :xhr => true
    assert_response :success
    assert_include 'Title has already been taken', response.body
  end

  def test_create_page
    @request.session[:user_id] = 2
    assert_difference 'WikiPage.count' do
      assert_difference 'WikiContent.count' do
        put :update, :params => {
          :project_id => 1,
          :id => 'New page',
          :content => {
            :comments => 'Created the page',
            :text => "h1. New page\n\nThis is a new page",
            :version => 0
          }
        }
      end
    end
    assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'New_page'
    page = Project.find(1).wiki.find_page('New page')
    assert !page.new_record?
    assert_not_nil page.content
    assert_nil page.parent
    assert_equal 'Created the page', page.content.comments
  end

  def test_create_page_with_attachments
    @request.session[:user_id] = 2
    assert_difference 'WikiPage.count' do
      assert_difference 'Attachment.count' do
        put :update, :params => {
          :project_id => 1,
          :id => 'New page',
          :content => {
            :comments => 'Created the page',
            :text => "h1. New page\n\nThis is a new page",
            :version => 0
          },
          :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
        }
      end
    end
    page = Project.find(1).wiki.find_page('New page')
    assert_equal 1, page.attachments.count
    assert_equal 'testfile.txt', page.attachments.first.filename
  end

  def test_create_page_with_parent
    @request.session[:user_id] = 2
    assert_difference 'WikiPage.count' do
      put :update, :params => {
        :project_id => 1,
        :id => 'New page',
        :content => {
          :text => "h1. New page\n\nThis is a new page",
          :version => 0
        },
        :wiki_page => {:parent_id => 2}
      }
    end
    page = Project.find(1).wiki.find_page('New page')
    assert_equal WikiPage.find(2), page.parent
  end

  def test_edit_page
    @request.session[:user_id] = 2
    get :edit, :params => {:project_id => 'ecookbook', :id => 'Another_page'}

    assert_response :success

    assert_select 'textarea[name=?]', 'content[text]',
      :text => WikiPage.find_by_title('Another_page').content.text
  end

  def test_edit_section
    @request.session[:user_id] = 2
    get :edit, :params => {:project_id => 'ecookbook', :id => 'Page_with_sections', :section => 2}

    assert_response :success

    page = WikiPage.find_by_title('Page_with_sections')
    section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)

    assert_select 'textarea[name=?]', 'content[text]', :text => section
    assert_select 'input[name=section][type=hidden][value="2"]'
    assert_select 'input[name=section_hash][type=hidden][value=?]', hash
  end

  def test_edit_invalid_section_should_respond_with_404
    @request.session[:user_id] = 2
    get :edit, :params => {:project_id => 'ecookbook', :id => 'Page_with_sections', :section => 10}

    assert_response 404
  end

  def test_update_page
    @request.session[:user_id] = 2
    assert_no_difference 'WikiPage.count' do
      assert_no_difference 'WikiContent.count' do
        assert_difference 'WikiContentVersion.count' do
          put :update, :params => {
            :project_id => 1,
            :id => 'Another_page',
            :content => {
              :comments => "my comments",
              :text => "edited",
              :version => 1
            }
          }
        end
      end
    end
    assert_redirected_to '/projects/ecookbook/wiki/Another_page'

    page = Wiki.find(1).pages.find_by_title('Another_page')
    assert_equal "edited", page.content.text
    assert_equal 2, page.content.version
    assert_equal "my comments", page.content.comments
  end

  def test_update_page_with_parent
    @request.session[:user_id] = 2
    assert_no_difference 'WikiPage.count' do
      assert_no_difference 'WikiContent.count' do
        assert_difference 'WikiContentVersion.count' do
          put :update, :params => {
            :project_id => 1,
            :id => 'Another_page',
            :content => {
              :comments => "my comments",
              :text => "edited",
              :version => 1
            },
            :wiki_page => {:parent_id => '1'}
          }
        end
      end
    end
    assert_redirected_to '/projects/ecookbook/wiki/Another_page'

    page = Wiki.find(1).pages.find_by_title('Another_page')
    assert_equal "edited", page.content.text
    assert_equal 2, page.content.version
    assert_equal "my comments", page.content.comments
    assert_equal WikiPage.find(1), page.parent
  end

  def test_update_page_with_failure
    @request.session[:user_id] = 2
    assert_no_difference 'WikiPage.count' do
      assert_no_difference 'WikiContent.count' do
        assert_no_difference 'WikiContentVersion.count' do
          put :update, :params => {
            :project_id => 1,
            :id => 'Another_page',
            :content => {
              :comments => 'a' * 1300,  # failure here, comment is too long
              :text => 'edited'
            },
            :wiki_page => {
              :parent_id => ""
            }
          }
        end
      end
    end
    assert_response :success

    assert_select_error /Comment is too long/
    assert_select 'textarea#content_text', :text => "edited"
    assert_select 'input#content_version[value="1"]'
  end

  def test_update_page_with_parent_change_only_should_not_create_content_version
    @request.session[:user_id] = 2
    assert_no_difference 'WikiPage.count' do
      assert_no_difference 'WikiContent.count' do
        assert_no_difference 'WikiContentVersion.count' do
          put :update, :params => {
            :project_id => 1,
            :id => 'Another_page',
            :content => {
              :comments => '',
              :text => Wiki.find(1).find_page('Another_page').content.text,
              :version => 1
            },
            :wiki_page => {:parent_id => '1'}
          }
        end
      end
    end
    page = Wiki.find(1).pages.find_by_title('Another_page')
    assert_equal 1, page.content.version
    assert_equal WikiPage.find(1), page.parent
  end

  def test_update_page_with_attachments_only_should_not_create_content_version
    @request.session[:user_id] = 2
    assert_no_difference 'WikiPage.count' do
      assert_no_difference 'WikiContent.count' do
        assert_no_difference 'WikiContentVersion.count' do
          assert_difference 'Attachment.count' do
            put :update, :params => {
              :project_id => 1,
              :id => 'Another_page',
              :content => {
                :comments => '',
                :text => Wiki.find(1).find_page('Another_page').content.text,
                :version => 1
              },
              :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
            }
          end
        end
      end
    end
    page = Wiki.find(1).pages.find_by_title('Another_page')
    assert_equal 1, page.content.version
  end

  def test_update_stale_page_should_not_raise_an_error
    @request.session[:user_id] = 2
    c = Wiki.find(1).find_page('Another_page').content
    c.text = 'Previous text'
    c.save!
    assert_equal 2, c.version

    assert_no_difference 'WikiPage.count' do
      assert_no_difference 'WikiContent.count' do
        assert_no_difference 'WikiContentVersion.count' do
          put :update, :params => {
            :project_id => 1,
            :id => 'Another_page',
            :content => {
              :comments => 'My comments',
              :text => 'Text should not be lost',
              :version => 1
            }
          }
        end
      end
    end
    assert_response :success
    assert_select 'div.error', :text => /Data has been updated by another user/
    assert_select 'textarea[name=?]', 'content[text]', :text => /Text should not be lost/
    assert_select 'input[name=?][value=?]', 'content[comments]', 'My comments'

    c.reload
    assert_equal 'Previous text', c.text
    assert_equal 2, c.version
  end

  def test_update_page_without_content_should_create_content
    @request.session[:user_id] = 2
    page = WikiPage.create!(:title => 'NoContent', :wiki => Project.find(1).wiki)

    assert_no_difference 'WikiPage.count' do
      assert_difference 'WikiContent.count' do
        put :update, :params => {
          :project_id => 1,
          :id => 'NoContent',
          :content => {:text => 'Some content'}
        }
        assert_response 302
      end
    end
    assert_equal 'Some content', page.reload.content.text
  end

  def test_update_section
    @request.session[:user_id] = 2
    page = WikiPage.find_by_title('Page_with_sections')
    section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
    text = page.content.text

    assert_no_difference 'WikiPage.count' do
      assert_no_difference 'WikiContent.count' do
        assert_difference 'WikiContentVersion.count' do
          put :update, :params => {
            :project_id => 1,
            :id => 'Page_with_sections',
            :content => {
              :text => "New section content",
              :version => 3
            },
            :section => 2,
            :section_hash => hash
          }
        end
      end
    end
    assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections#section-2'
    assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.reload.content.text
  end

  def test_update_section_should_allow_stale_page_update
    @request.session[:user_id] = 2
    page = WikiPage.find_by_title('Page_with_sections')
    section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
    text = page.content.text

    assert_no_difference 'WikiPage.count' do
      assert_no_difference 'WikiContent.count' do
        assert_difference 'WikiContentVersion.count' do
          put :update, :params => {
            :project_id => 1,
            :id => 'Page_with_sections',
            :content => {
              :text => "New section content",
              :version => 2 # Current version is 3
            },
            :section => 2,
            :section_hash => hash
          }
        end
      end
    end
    assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections#section-2'
    page.reload
    assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.content.text
    assert_equal 4, page.content.version
  end

  def test_update_section_should_not_allow_stale_section_update
    @request.session[:user_id] = 2

    assert_no_difference 'WikiPage.count' do
      assert_no_difference 'WikiContent.count' do
        assert_no_difference 'WikiContentVersion.count' do
          put :update, :params => {
            :project_id => 1,
            :id => 'Page_with_sections',
            :content => {
              :comments => 'My comments',
              :text => "Text should not be lost",
              :version => 3
            },
            :section => 2,
            :section_hash => Digest::MD5.hexdigest("wrong hash")
          }
        end
      end
    end
    assert_response :success
    assert_select 'div.error', :text => /Data has been updated by another user/
    assert_select 'textarea[name=?]', 'content[text]', :text => /Text should not be lost/
    assert_select 'input[name=?][value=?]', 'content[comments]', 'My comments'
  end

  def test_preview
    @request.session[:user_id] = 2
    post :preview, :params => {
      :project_id => 1,
      :id => 'CookBook_documentation',
      :content => {
        :comments => '',
        :text => 'this is a *previewed text*',
        :version => 3
      }
    }, :xhr => true
    assert_response :success
    assert_select 'strong', :text => /previewed text/
  end

  def test_preview_new_page
    @request.session[:user_id] = 2
    post :preview, :params => {
      :project_id => 1,
      :id => 'New page',
      :content => {
        :text => 'h1. New page',
        :comments => '',
        :version => 0
      }
    }, :xhr => true
    assert_response :success
    assert_select 'h1', :text => /New page/
  end

  def test_history
    @request.session[:user_id] = 2
    get :history, :params => {:project_id => 'ecookbook', :id => 'CookBook_documentation'}
    assert_response :success

    assert_select 'table.wiki-page-versions tbody' do
      assert_select 'tr', 3
    end

    assert_select "input[type=submit][name=commit]"
    assert_select 'td' do
      assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => '2'
      assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2/annotate', :text => 'Annotate'
      assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => 'Delete'
    end
  end

  def test_history_with_one_version
    @request.session[:user_id] = 2
    get :history, :params => {:project_id => 'ecookbook', :id => 'Another_page'}
    assert_response :success

    assert_select 'table.wiki-page-versions tbody' do
      assert_select 'tr', 1
    end

    assert_select "input[type=submit][name=commit]", false
    assert_select 'td' do
      assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1', :text => '1'
      assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1/annotate', :text => 'Annotate'
      assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1', :text => 'Delete', :count => 0
    end
  end

  def test_diff
    content = WikiPage.find(1).content
    assert_difference 'WikiContentVersion.count', 2 do
      content.text = "Line removed\nThis is a sample text for testing diffs"
      content.save!
      content.text = "This is a sample text for testing diffs\nLine added"
      content.save!
    end

    get :diff, :params => {
      :project_id => 1, :id => 'CookBook_documentation',
      :version => content.version,
      :version_from => (content.version - 1)
    }
    assert_response :success
    assert_select 'span.diff_out', :text => 'Line removed'
    assert_select 'span.diff_in', :text => 'Line added'
  end

  def test_diff_with_invalid_version_should_respond_with_404
    get :diff, :params => {
      :project_id => 1, :id => 'CookBook_documentation',
      :version => '99'
    }
    assert_response 404
  end

  def test_diff_with_invalid_version_from_should_respond_with_404
    get :diff, :params => {
      :project_id => 1, :id => 'CookBook_documentation',
      :version => '99',
      :version_from => '98'
    }
    assert_response 404
  end

  def test_annotate
    get :annotate, :params => {
      :project_id => 1, :id =>  'CookBook_documentation',
      :version => 2
    }
    assert_response :success

    # Line 1
    assert_select 'table.annotate tr:nth-child(1)' do
      assert_select 'th.line-num', :text => '1'
      assert_select 'td.author', :text => /Redmine Admin/
      assert_select 'td', :text => /h1\. CookBook documentation v2/
    end

    # Line 4
    assert_select 'table.annotate tr:nth-child(4)' do
      assert_select 'th.line-num', :text => '4'
      assert_select 'td.author', :text => /John Smith/
      assert_select 'td', :text => /Line from v1/
    end

    # Line 5
    assert_select 'table.annotate tr:nth-child(5)' do
      assert_select 'th.line-num', :text => '5'
      assert_select 'td.author', :text => /Redmine Admin/
      assert_select 'td', :text => /Some updated \[\[documentation\]\] here/
    end
  end

  def test_annotate_with_invalid_version_should_respond_with_404
    get :annotate, :params => {
      :project_id => 1, :id => 'CookBook_documentation',
      :version => '99'
    }
    assert_response 404
  end

  def test_get_rename
    @request.session[:user_id] = 2
    get :rename, :params => {:project_id => 1, :id => 'Another_page'}
    assert_response :success

    assert_select 'select[name=?]', 'wiki_page[parent_id]' do
      assert_select 'option[value=""]', :text => ''
      assert_select 'option[selected=selected]', 0
    end
  end

  def test_get_rename_child_page
    @request.session[:user_id] = 2
    get :rename, :params => {:project_id => 1, :id => 'Child_1'}
    assert_response :success

    assert_select 'select[name=?]', 'wiki_page[parent_id]' do
      assert_select 'option[value=""]', :text => ''
      assert_select 'option[value="2"][selected=selected]', :text => /Another page/
    end
  end

  def test_rename_with_redirect
    @request.session[:user_id] = 2
    post :rename, :params => {
      :project_id => 1,
      :id => 'Another_page',
      :wiki_page => {
        :title => 'Another renamed page',
        :redirect_existing_links => 1
      }
    }
    assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
    wiki = Project.find(1).wiki
    # Check redirects
    assert_not_nil wiki.find_page('Another page')
    assert_nil wiki.find_page('Another page', :with_redirect => false)
  end

  def test_rename_without_redirect
    @request.session[:user_id] = 2
    post :rename, :params => {
      :project_id => 1,
      :id => 'Another_page',
      :wiki_page => {
        :title => 'Another renamed page',
        :redirect_existing_links => "0"
      }
    }
    assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
    wiki = Project.find(1).wiki
    # Check that there's no redirects
    assert_nil wiki.find_page('Another page')
  end

  def test_rename_with_parent_assignment
    @request.session[:user_id] = 2
    post :rename, :params => {
      :project_id => 1,
      :id => 'Another_page',
      :wiki_page => {
        :title => 'Another page',
        :redirect_existing_links => "0",
        :parent_id => '4'
      }
    }
    assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
    assert_equal WikiPage.find(4), WikiPage.find_by_title('Another_page').parent
  end

  def test_rename_with_parent_unassignment
    @request.session[:user_id] = 2
    post :rename, :params => {
      :project_id => 1,
      :id => 'Child_1',
      :wiki_page => {
        :title => 'Child 1',
        :redirect_existing_links => "0",
        :parent_id => ''
      }
    }
    assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Child_1'
    assert_nil WikiPage.find_by_title('Child_1').parent
  end

  def test_get_rename_should_show_target_projects_list
    @request.session[:user_id] = 2
    project = Project.find(5)
    project.enable_module! :wiki

    get :rename, :params => {:project_id => 1, :id => 'Another_page'}
    assert_response :success

    assert_select 'select[name=?]', 'wiki_page[wiki_id]' do
      assert_select 'option', 2
      assert_select 'option[value=?][selected=selected]', '1', :text => /eCookbook/
      assert_select 'option[value=?]', project.wiki.id.to_s, :text => /#{project.name}/
    end
  end

  def test_rename_with_move
    @request.session[:user_id] = 2
    project = Project.find(5)
    project.enable_module! :wiki

    post :rename, :params => {
      :project_id => 1,
      :id => 'Another_page',
      :wiki_page => {
        :wiki_id => project.wiki.id.to_s,
        :title => 'Another renamed page',
        :redirect_existing_links => 1
      }
    }
    assert_redirected_to '/projects/private-child/wiki/Another_renamed_page'

    page = WikiPage.find(2)
    assert_equal project.wiki.id, page.wiki_id
  end

  def test_rename_as_start_page
    @request.session[:user_id] = 2

    post :rename, :params => {
      :project_id => 'ecookbook',
      :id => 'Another_page',
      :wiki_page => {
        :wiki_id => '1',
        :title => 'Another_page',
        :redirect_existing_links => '1',
        :is_start_page => '1'
      }
    }
    assert_redirected_to '/projects/ecookbook/wiki/Another_page'

    wiki = Wiki.find(1)
    assert_equal 'Another_page', wiki.start_page
  end

  def test_destroy_a_page_without_children_should_not_ask_confirmation
    @request.session[:user_id] = 2
    delete :destroy, :params => {:project_id => 1, :id => 'Child_2'}
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
  end

  def test_destroy_parent_should_ask_confirmation
    @request.session[:user_id] = 2
    assert_no_difference('WikiPage.count') do
      delete :destroy, :params => {:project_id => 1, :id => 'Another_page'}
    end
    assert_response :success
    assert_select 'form' do
      assert_select 'input[name=todo][value=nullify]'
      assert_select 'input[name=todo][value=destroy]'
      assert_select 'input[name=todo][value=reassign]'
    end
  end

  def test_destroy_parent_with_nullify_should_delete_parent_only
    @request.session[:user_id] = 2
    assert_difference('WikiPage.count', -1) do
      delete :destroy, :params => {:project_id => 1, :id => 'Another_page', :todo => 'nullify'}
    end
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
    assert_nil WikiPage.find_by_id(2)
  end

  def test_destroy_parent_with_cascade_should_delete_descendants
    @request.session[:user_id] = 2
    assert_difference('WikiPage.count', -4) do
      delete :destroy, :params => {:project_id => 1, :id => 'Another_page', :todo => 'destroy'}
    end
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
    assert_nil WikiPage.find_by_id(2)
    assert_nil WikiPage.find_by_id(5)
  end

  def test_destroy_parent_with_reassign
    @request.session[:user_id] = 2
    assert_difference('WikiPage.count', -1) do
      delete :destroy, :params => {:project_id => 1, :id => 'Another_page', :todo => 'reassign', :reassign_to_id => 1}
    end
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
    assert_nil WikiPage.find_by_id(2)
    assert_equal WikiPage.find(1), WikiPage.find_by_id(5).parent
  end

  def test_destroy_version
    @request.session[:user_id] = 2
    assert_difference 'WikiContentVersion.count', -1 do
      assert_no_difference 'WikiContent.count' do
        assert_no_difference 'WikiPage.count' do
          delete :destroy_version, :params => {:project_id => 'ecookbook', :id => 'CookBook_documentation', :version => 2}
          assert_redirected_to '/projects/ecookbook/wiki/CookBook_documentation/history'
        end
      end
    end
  end

  def test_destroy_invalid_version_should_respond_with_404
    @request.session[:user_id] = 2
    assert_no_difference 'WikiContentVersion.count' do
      assert_no_difference 'WikiContent.count' do
        assert_no_difference 'WikiPage.count' do
          delete :destroy_version, :params => {:project_id => 'ecookbook', :id => 'CookBook_documentation', :version => 99}
        end
      end
    end
    assert_response 404
  end

  def test_index
    get :index, :params => {:project_id => 'ecookbook'}
    assert_response :success

    assert_select 'ul.pages-hierarchy' do
      assert_select 'li', Project.find(1).wiki.pages.count
    end

    assert_select 'ul.pages-hierarchy' do
      assert_select 'li' do
        assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation', :text => 'CookBook documentation'
        assert_select 'ul li a[href=?]', '/projects/ecookbook/wiki/Page_with_an_inline_image', :text => 'Page with an inline image'
      end
      assert_select 'li a[href=?]', '/projects/ecookbook/wiki/Another_page', :text => 'Another page'
    end
  end

  def test_index_should_include_atom_link
    get :index, :params => {:project_id => 'ecookbook'}
    assert_select 'a[href=?]', '/projects/ecookbook/activity.atom?show_wiki_edits=1'
  end

  def test_export_to_html
    @request.session[:user_id] = 2
    get :export, :params => {:project_id => 'ecookbook'}

    assert_response :success
    assert_equal "text/html", @response.content_type

    assert_select "a[name=?]", "CookBook_documentation"
    assert_select "a[name=?]", "Another_page"
    assert_select "a[name=?]", "Page_with_an_inline_image"
  end

  def test_export_to_pdf
    @request.session[:user_id] = 2
    get :export, :params => {:project_id => 'ecookbook', :format => 'pdf'}

    assert_response :success
    assert_equal 'application/pdf', @response.content_type
    assert_equal 'attachment; filename="ecookbook.pdf"', @response.headers['Content-Disposition']
    assert @response.body.starts_with?('%PDF')
  end

  def test_export_without_permission_should_be_denied
    @request.session[:user_id] = 2
    Role.find_by_name('Manager').remove_permission! :export_wiki_pages
    get :export, :params => {:project_id => 'ecookbook'}

    assert_response 403
  end

  def test_date_index
    get :date_index, :params => {:project_id => 'ecookbook'}

    assert_response :success

    assert_select 'a[href=?]', '/projects/ecookbook/activity.atom?show_wiki_edits=1'
  end

  def test_not_found
    get :show, :params => {:project_id => 999}
    assert_response 404
  end

  def test_protect_page
    page = WikiPage.find_by_wiki_id_and_title(1, 'Another_page')
    assert !page.protected?
    @request.session[:user_id] = 2
    post :protect, :params => {:project_id => 1, :id => page.title, :protected => '1'}
    assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
    assert page.reload.protected?
  end

  def test_unprotect_page
    page = WikiPage.find_by_wiki_id_and_title(1, 'CookBook_documentation')
    assert page.protected?
    @request.session[:user_id] = 2
    post :protect, :params => {:project_id => 1, :id => page.title, :protected => '0'}
    assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'CookBook_documentation'
    assert !page.reload.protected?
  end

  def test_show_page_with_edit_link
    @request.session[:user_id] = 2
    get :show, :params => {:project_id => 1}
    assert_response :success

    assert_select 'a[href=?]', '/projects/1/wiki/CookBook_documentation/edit'
  end

  def test_show_page_without_edit_link
    @request.session[:user_id] = 4
    get :show, :params => {:project_id => 1}
    assert_response :success

    assert_select 'a[href=?]', '/projects/1/wiki/CookBook_documentation/edit', 0
  end

  def test_show_pdf
    @request.session[:user_id] = 2
    get :show, :params => {:project_id => 1, :format => 'pdf'}
    assert_response :success

    assert_equal 'application/pdf', @response.content_type
    assert_equal 'attachment; filename="CookBook_documentation.pdf"',
                  @response.headers['Content-Disposition']
  end

  def test_show_html
    @request.session[:user_id] = 2
    get :show, :params => {:project_id => 1, :format => 'html'}
    assert_response :success

    assert_equal 'text/html', @response.content_type
    assert_equal 'attachment; filename="CookBook_documentation.html"',
                  @response.headers['Content-Disposition']
    assert_select 'h1', :text => /CookBook documentation/
  end

  def test_show_versioned_html
    @request.session[:user_id] = 2
    get :show, :params => {:project_id => 1, :format => 'html', :version => 2}
    assert_response :success

    assert_equal 'text/html', @response.content_type
    assert_equal 'attachment; filename="CookBook_documentation.html"',
                  @response.headers['Content-Disposition']
    assert_select 'h1', :text => /CookBook documentation v2/
  end

  def test_show_txt
    @request.session[:user_id] = 2
    get :show, :params => {:project_id => 1, :format => 'txt'}
    assert_response :success

    assert_equal 'text/plain', @response.content_type
    assert_equal 'attachment; filename="CookBook_documentation.txt"',
                  @response.headers['Content-Disposition']
    assert_include 'h1. CookBook documentation', @response.body
  end

  def test_show_versioned_txt
    @request.session[:user_id] = 2
    get :show, :params => {:project_id => 1, :format => 'txt', :version => 2}
    assert_response :success

    assert_equal 'text/plain', @response.content_type
    assert_equal 'attachment; filename="CookBook_documentation.txt"',
                  @response.headers['Content-Disposition']
    assert_include 'h1. CookBook documentation v2', @response.body
  end

  def test_show_filename_should_be_uri_encoded_for_ms_browsers
    @request.session[:user_id] = 2
    title = 'Этика_менеджмента'
    %w|pdf html txt|.each do |format|
      # Non-MS browsers
      @request.user_agent = ""
      get :show, :params => {:project_id => 1, :id => title, :format => format}
      assert_response :success
      assert_equal "attachment; filename=\"#{title}.#{format}\"",
                    @response.headers['Content-Disposition']
      # Microsoft's browsers: filename should be URI encoded
      @request.user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'
      get :show, :params => {:project_id => 1, :id => title, :format => format}
      assert_response :success
      filename = URI.encode("#{title}.#{format}")
      assert_equal "attachment; filename=\"#{filename}\"",
                    @response.headers['Content-Disposition']
    end
  end

  def test_edit_unprotected_page
    # Non members can edit unprotected wiki pages
    @request.session[:user_id] = 4
    get :edit, :params => {:project_id => 1, :id => 'Another_page'}
    assert_response :success
  end

  def test_edit_protected_page_by_nonmember
    # Non members cannot edit protected wiki pages
    @request.session[:user_id] = 4
    get :edit, :params => {:project_id => 1, :id => 'CookBook_documentation'}
    assert_response 403
  end

  def test_edit_protected_page_by_member
    @request.session[:user_id] = 2
    get :edit, :params => {:project_id => 1, :id => 'CookBook_documentation'}
    assert_response :success
  end

  def test_history_of_non_existing_page_should_return_404
    get :history, :params => {:project_id => 1, :id => 'Unknown_page'}
    assert_response 404
  end

  def test_add_attachment
    @request.session[:user_id] = 2
    assert_difference 'Attachment.count' do
      post :add_attachment, :params => {
        :project_id => 1,
        :id => 'CookBook_documentation',
        :attachments => {
          '1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}
        }
      }
    end
    attachment = Attachment.order('id DESC').first
    assert_equal Wiki.find(1).find_page('CookBook_documentation'), attachment.container
  end
end