To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 69 / 694cd2c99623d577c90a26a298a76c97fefa807e.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (34.7 KB)

1 1296:038ba2d95de8 Chris
# Redmine - project management software
2
# Copyright (C) 2006-2012  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18
require File.expand_path('../../test_helper', __FILE__)
19
require 'wiki_controller'
20
21
# Re-raise errors caught by the controller.
22
class WikiController; def rescue_action(e) raise e end; end
23
24
class WikiControllerTest < ActionController::TestCase
25
  fixtures :projects, :users, :roles, :members, :member_roles,
26
           :enabled_modules, :wikis, :wiki_pages, :wiki_contents,
27
           :wiki_content_versions, :attachments
28
29
  def setup
30
    @controller = WikiController.new
31
    @request    = ActionController::TestRequest.new
32
    @response   = ActionController::TestResponse.new
33
    User.current = nil
34
  end
35
36
  def test_show_start_page
37
    get :show, :project_id => 'ecookbook'
38
    assert_response :success
39
    assert_template 'show'
40
    assert_tag :tag => 'h1', :content => /CookBook documentation/
41
42
    # child_pages macro
43
    assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
44
               :child => { :tag => 'li',
45
                           :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
46
                                                    :content => 'Page with an inline image' } }
47
  end
48
49
  def test_export_link
50
    Role.anonymous.add_permission! :export_wiki_pages
51
    get :show, :project_id => 'ecookbook'
52
    assert_response :success
53
    assert_tag 'a', :attributes => {:href => '/projects/ecookbook/wiki/CookBook_documentation.txt'}
54
  end
55
56
  def test_show_page_with_name
57
    get :show, :project_id => 1, :id => 'Another_page'
58
    assert_response :success
59
    assert_template 'show'
60
    assert_tag :tag => 'h1', :content => /Another page/
61
    # Included page with an inline image
62
    assert_tag :tag => 'p', :content => /This is an inline image/
63
    assert_tag :tag => 'img', :attributes => { :src => '/attachments/download/3',
64
                                               :alt => 'This is a logo' }
65
  end
66
67
  def test_show_old_version
68
    get :show, :project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '2'
69
    assert_response :success
70
    assert_template 'show'
71
72
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/1', :text => /Previous/
73
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2/diff', :text => /diff/
74
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/3', :text => /Next/
75
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation', :text => /Current version/
76
  end
77
78
  def test_show_old_version_without_permission_should_be_denied
79
    Role.anonymous.remove_permission! :view_wiki_edits
80
81
    get :show, :project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '2'
82
    assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fprojects%2Fecookbook%2Fwiki%2FCookBook_documentation%2F2'
83
  end
84
85
  def test_show_first_version
86
    get :show, :project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '1'
87
    assert_response :success
88
    assert_template 'show'
89
90
    assert_select 'a', :text => /Previous/, :count => 0
91
    assert_select 'a', :text => /diff/, :count => 0
92
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => /Next/
93
    assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation', :text => /Current version/
94
  end
95
96
  def test_show_redirected_page
97
    WikiRedirect.create!(:wiki_id => 1, :title => 'Old_title', :redirects_to => 'Another_page')
98
99
    get :show, :project_id => 'ecookbook', :id => 'Old_title'
100
    assert_redirected_to '/projects/ecookbook/wiki/Another_page'
101
  end
102
103
  def test_show_with_sidebar
104
    page = Project.find(1).wiki.pages.new(:title => 'Sidebar')
105
    page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
106
    page.save!
107
108
    get :show, :project_id => 1, :id => 'Another_page'
109
    assert_response :success
110
    assert_tag :tag => 'div', :attributes => {:id => 'sidebar'},
111
                              :content => /Side bar content for test_show_with_sidebar/
112
  end
113
114
  def test_show_should_display_section_edit_links
115
    @request.session[:user_id] = 2
116
    get :show, :project_id => 1, :id => 'Page with sections'
117
    assert_no_tag 'a', :attributes => {
118
      :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=1'
119
    }
120
    assert_tag 'a', :attributes => {
121
      :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
122
    }
123
    assert_tag 'a', :attributes => {
124
      :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=3'
125
    }
126
  end
127
128
  def test_show_current_version_should_display_section_edit_links
129
    @request.session[:user_id] = 2
130
    get :show, :project_id => 1, :id => 'Page with sections', :version => 3
131
132
    assert_tag 'a', :attributes => {
133
      :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
134
    }
135
  end
136
137
  def test_show_old_version_should_not_display_section_edit_links
138
    @request.session[:user_id] = 2
139
    get :show, :project_id => 1, :id => 'Page with sections', :version => 2
140
141
    assert_no_tag 'a', :attributes => {
142
      :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
143
    }
144
  end
145
146
  def test_show_unexistent_page_without_edit_right
147
    get :show, :project_id => 1, :id => 'Unexistent page'
148
    assert_response 404
149
  end
150
151
  def test_show_unexistent_page_with_edit_right
152
    @request.session[:user_id] = 2
153
    get :show, :project_id => 1, :id => 'Unexistent page'
154
    assert_response :success
155
    assert_template 'edit'
156
  end
157
158
  def test_show_unexistent_page_with_parent_should_preselect_parent
159
    @request.session[:user_id] = 2
160
    get :show, :project_id => 1, :id => 'Unexistent page', :parent => 'Another_page'
161
    assert_response :success
162
    assert_template 'edit'
163
    assert_tag 'select', :attributes => {:name => 'wiki_page[parent_id]'},
164
      :child => {:tag => 'option', :attributes => {:value => '2', :selected => 'selected'}}
165
  end
166
167
  def test_show_should_not_show_history_without_permission
168
    Role.anonymous.remove_permission! :view_wiki_edits
169
    get :show, :project_id => 1, :id => 'Page with sections', :version => 2
170
171
    assert_response 302
172
  end
173
174
  def test_create_page
175
    @request.session[:user_id] = 2
176
    assert_difference 'WikiPage.count' do
177
      assert_difference 'WikiContent.count' do
178
        put :update, :project_id => 1,
179
                    :id => 'New page',
180
                    :content => {:comments => 'Created the page',
181
                                 :text => "h1. New page\n\nThis is a new page",
182
                                 :version => 0}
183
      end
184
    end
185
    assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'New_page'
186
    page = Project.find(1).wiki.find_page('New page')
187
    assert !page.new_record?
188
    assert_not_nil page.content
189
    assert_nil page.parent
190
    assert_equal 'Created the page', page.content.comments
191
  end
192
193
  def test_create_page_with_attachments
194
    @request.session[:user_id] = 2
195
    assert_difference 'WikiPage.count' do
196
      assert_difference 'Attachment.count' do
197
        put :update, :project_id => 1,
198
                    :id => 'New page',
199
                    :content => {:comments => 'Created the page',
200
                                 :text => "h1. New page\n\nThis is a new page",
201
                                 :version => 0},
202
                    :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
203
      end
204
    end
205
    page = Project.find(1).wiki.find_page('New page')
206
    assert_equal 1, page.attachments.count
207
    assert_equal 'testfile.txt', page.attachments.first.filename
208
  end
209
210
  def test_create_page_with_parent
211
    @request.session[:user_id] = 2
212
    assert_difference 'WikiPage.count' do
213
      put :update, :project_id => 1, :id => 'New page',
214
        :content => {:text => "h1. New page\n\nThis is a new page", :version => 0},
215
        :wiki_page => {:parent_id => 2}
216
    end
217
    page = Project.find(1).wiki.find_page('New page')
218
    assert_equal WikiPage.find(2), page.parent
219
  end
220
221
  def test_edit_page
222
    @request.session[:user_id] = 2
223
    get :edit, :project_id => 'ecookbook', :id => 'Another_page'
224
225
    assert_response :success
226
    assert_template 'edit'
227
228
    assert_tag 'textarea',
229
      :attributes => { :name => 'content[text]' },
230
      :content => "\n"+WikiPage.find_by_title('Another_page').content.text
231
  end
232
233
  def test_edit_section
234
    @request.session[:user_id] = 2
235
    get :edit, :project_id => 'ecookbook', :id => 'Page_with_sections', :section => 2
236
237
    assert_response :success
238
    assert_template 'edit'
239
240
    page = WikiPage.find_by_title('Page_with_sections')
241
    section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
242
243
    assert_tag 'textarea',
244
      :attributes => { :name => 'content[text]' },
245
      :content => "\n"+section
246
    assert_tag 'input',
247
      :attributes => { :name => 'section', :type => 'hidden', :value => '2' }
248
    assert_tag 'input',
249
      :attributes => { :name => 'section_hash', :type => 'hidden', :value => hash }
250
  end
251
252
  def test_edit_invalid_section_should_respond_with_404
253
    @request.session[:user_id] = 2
254
    get :edit, :project_id => 'ecookbook', :id => 'Page_with_sections', :section => 10
255
256
    assert_response 404
257
  end
258
259
  def test_update_page
260
    @request.session[:user_id] = 2
261
    assert_no_difference 'WikiPage.count' do
262
      assert_no_difference 'WikiContent.count' do
263
        assert_difference 'WikiContent::Version.count' do
264
          put :update, :project_id => 1,
265
            :id => 'Another_page',
266
            :content => {
267
              :comments => "my comments",
268
              :text => "edited",
269
              :version => 1
270
            }
271
        end
272
      end
273
    end
274
    assert_redirected_to '/projects/ecookbook/wiki/Another_page'
275
276
    page = Wiki.find(1).pages.find_by_title('Another_page')
277
    assert_equal "edited", page.content.text
278
    assert_equal 2, page.content.version
279
    assert_equal "my comments", page.content.comments
280
  end
281
282
  def test_update_page_with_parent
283
    @request.session[:user_id] = 2
284
    assert_no_difference 'WikiPage.count' do
285
      assert_no_difference 'WikiContent.count' do
286
        assert_difference 'WikiContent::Version.count' do
287
          put :update, :project_id => 1,
288
            :id => 'Another_page',
289
            :content => {
290
              :comments => "my comments",
291
              :text => "edited",
292
              :version => 1
293
            },
294
            :wiki_page => {:parent_id => '1'}
295
        end
296
      end
297
    end
298
    assert_redirected_to '/projects/ecookbook/wiki/Another_page'
299
300
    page = Wiki.find(1).pages.find_by_title('Another_page')
301
    assert_equal "edited", page.content.text
302
    assert_equal 2, page.content.version
303
    assert_equal "my comments", page.content.comments
304
    assert_equal WikiPage.find(1), page.parent
305
  end
306
307
  def test_update_page_with_failure
308
    @request.session[:user_id] = 2
309
    assert_no_difference 'WikiPage.count' do
310
      assert_no_difference 'WikiContent.count' do
311
        assert_no_difference 'WikiContent::Version.count' do
312
          put :update, :project_id => 1,
313
            :id => 'Another_page',
314
            :content => {
315
              :comments => 'a' * 300,  # failure here, comment is too long
316
              :text => 'edited',
317
              :version => 1
318
            }
319
          end
320
        end
321
      end
322
    assert_response :success
323
    assert_template 'edit'
324
325
    assert_error_tag :descendant => {:content => /Comment is too long/}
326
    assert_tag :tag => 'textarea', :attributes => {:id => 'content_text'}, :content => "\nedited"
327
    assert_tag :tag => 'input', :attributes => {:id => 'content_version', :value => '1'}
328
  end
329
330
  def test_update_page_with_parent_change_only_should_not_create_content_version
331
    @request.session[:user_id] = 2
332
    assert_no_difference 'WikiPage.count' do
333
      assert_no_difference 'WikiContent.count' do
334
        assert_no_difference 'WikiContent::Version.count' do
335
          put :update, :project_id => 1,
336
            :id => 'Another_page',
337
            :content => {
338
              :comments => '',
339
              :text => Wiki.find(1).find_page('Another_page').content.text,
340
              :version => 1
341
            },
342
            :wiki_page => {:parent_id => '1'}
343
        end
344
      end
345
    end
346
    page = Wiki.find(1).pages.find_by_title('Another_page')
347
    assert_equal 1, page.content.version
348
    assert_equal WikiPage.find(1), page.parent
349
  end
350
351
  def test_update_page_with_attachments_only_should_not_create_content_version
352
    @request.session[:user_id] = 2
353
    assert_no_difference 'WikiPage.count' do
354
      assert_no_difference 'WikiContent.count' do
355
        assert_no_difference 'WikiContent::Version.count' do
356
          assert_difference 'Attachment.count' do
357
            put :update, :project_id => 1,
358
              :id => 'Another_page',
359
              :content => {
360
                :comments => '',
361
                :text => Wiki.find(1).find_page('Another_page').content.text,
362
                :version => 1
363
              },
364
              :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
365
          end
366
        end
367
      end
368
    end
369
    page = Wiki.find(1).pages.find_by_title('Another_page')
370
    assert_equal 1, page.content.version
371
  end
372
373
  def test_update_stale_page_should_not_raise_an_error
374
    @request.session[:user_id] = 2
375
    c = Wiki.find(1).find_page('Another_page').content
376
    c.text = 'Previous text'
377
    c.save!
378
    assert_equal 2, c.version
379
380
    assert_no_difference 'WikiPage.count' do
381
      assert_no_difference 'WikiContent.count' do
382
        assert_no_difference 'WikiContent::Version.count' do
383
          put :update, :project_id => 1,
384
            :id => 'Another_page',
385
            :content => {
386
              :comments => 'My comments',
387
              :text => 'Text should not be lost',
388
              :version => 1
389
            }
390
        end
391
      end
392
    end
393
    assert_response :success
394
    assert_template 'edit'
395
    assert_tag :div,
396
      :attributes => { :class => /error/ },
397
      :content => /Data has been updated by another user/
398
    assert_tag 'textarea',
399
      :attributes => { :name => 'content[text]' },
400
      :content => /Text should not be lost/
401
    assert_tag 'input',
402
      :attributes => { :name => 'content[comments]', :value => 'My comments' }
403
404
    c.reload
405
    assert_equal 'Previous text', c.text
406
    assert_equal 2, c.version
407
  end
408
409
  def test_update_section
410
    @request.session[:user_id] = 2
411
    page = WikiPage.find_by_title('Page_with_sections')
412
    section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
413
    text = page.content.text
414
415
    assert_no_difference 'WikiPage.count' do
416
      assert_no_difference 'WikiContent.count' do
417
        assert_difference 'WikiContent::Version.count' do
418
          put :update, :project_id => 1, :id => 'Page_with_sections',
419
            :content => {
420
              :text => "New section content",
421
              :version => 3
422
            },
423
            :section => 2,
424
            :section_hash => hash
425
        end
426
      end
427
    end
428
    assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections'
429
    assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.reload.content.text
430
  end
431
432
  def test_update_section_should_allow_stale_page_update
433
    @request.session[:user_id] = 2
434
    page = WikiPage.find_by_title('Page_with_sections')
435
    section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
436
    text = page.content.text
437
438
    assert_no_difference 'WikiPage.count' do
439
      assert_no_difference 'WikiContent.count' do
440
        assert_difference 'WikiContent::Version.count' do
441
          put :update, :project_id => 1, :id => 'Page_with_sections',
442
            :content => {
443
              :text => "New section content",
444
              :version => 2 # Current version is 3
445
            },
446
            :section => 2,
447
            :section_hash => hash
448
        end
449
      end
450
    end
451
    assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections'
452
    page.reload
453
    assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.content.text
454
    assert_equal 4, page.content.version
455
  end
456
457
  def test_update_section_should_not_allow_stale_section_update
458
    @request.session[:user_id] = 2
459
460
    assert_no_difference 'WikiPage.count' do
461
      assert_no_difference 'WikiContent.count' do
462
        assert_no_difference 'WikiContent::Version.count' do
463
          put :update, :project_id => 1, :id => 'Page_with_sections',
464
            :content => {
465
              :comments => 'My comments',
466
              :text => "Text should not be lost",
467
              :version => 3
468
            },
469
            :section => 2,
470
            :section_hash => Digest::MD5.hexdigest("wrong hash")
471
        end
472
      end
473
    end
474
    assert_response :success
475
    assert_template 'edit'
476
    assert_tag :div,
477
      :attributes => { :class => /error/ },
478
      :content => /Data has been updated by another user/
479
    assert_tag 'textarea',
480
      :attributes => { :name => 'content[text]' },
481
      :content => /Text should not be lost/
482
    assert_tag 'input',
483
      :attributes => { :name => 'content[comments]', :value => 'My comments' }
484
  end
485
486
  def test_preview
487
    @request.session[:user_id] = 2
488
    xhr :post, :preview, :project_id => 1, :id => 'CookBook_documentation',
489
                                   :content => { :comments => '',
490
                                                 :text => 'this is a *previewed text*',
491
                                                 :version => 3 }
492
    assert_response :success
493
    assert_template 'common/_preview'
494
    assert_tag :tag => 'strong', :content => /previewed text/
495
  end
496
497
  def test_preview_new_page
498
    @request.session[:user_id] = 2
499
    xhr :post, :preview, :project_id => 1, :id => 'New page',
500
                                   :content => { :text => 'h1. New page',
501
                                                 :comments => '',
502
                                                 :version => 0 }
503
    assert_response :success
504
    assert_template 'common/_preview'
505
    assert_tag :tag => 'h1', :content => /New page/
506
  end
507
508
  def test_history
509
    @request.session[:user_id] = 2
510
    get :history, :project_id => 'ecookbook', :id => 'CookBook_documentation'
511
    assert_response :success
512
    assert_template 'history'
513
    assert_not_nil assigns(:versions)
514
    assert_equal 3, assigns(:versions).size
515
516
    assert_select "input[type=submit][name=commit]"
517
    assert_select 'td' do
518
      assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => '2'
519
      assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2/annotate', :text => 'Annotate'
520
      assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => 'Delete'
521
    end
522
  end
523
524
  def test_history_with_one_version
525
    @request.session[:user_id] = 2
526
    get :history, :project_id => 'ecookbook', :id => 'Another_page'
527
    assert_response :success
528
    assert_template 'history'
529
    assert_not_nil assigns(:versions)
530
    assert_equal 1, assigns(:versions).size
531
    assert_select "input[type=submit][name=commit]", false
532
    assert_select 'td' do
533
      assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1', :text => '1'
534
      assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1/annotate', :text => 'Annotate'
535
      assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1', :text => 'Delete', :count => 0
536
    end
537
  end
538
539
  def test_diff
540
    content = WikiPage.find(1).content
541
    assert_difference 'WikiContent::Version.count', 2 do
542
      content.text = "Line removed\nThis is a sample text for testing diffs"
543
      content.save!
544
      content.text = "This is a sample text for testing diffs\nLine added"
545
      content.save!
546
    end
547
548
    get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => content.version, :version_from => (content.version - 1)
549
    assert_response :success
550
    assert_template 'diff'
551
    assert_select 'span.diff_out', :text => 'Line removed'
552
    assert_select 'span.diff_in', :text => 'Line added'
553
  end
554
555
  def test_diff_with_invalid_version_should_respond_with_404
556
    get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => '99'
557
    assert_response 404
558
  end
559
560
  def test_diff_with_invalid_version_from_should_respond_with_404
561
    get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => '99', :version_from => '98'
562
    assert_response 404
563
  end
564
565
  def test_annotate
566
    get :annotate, :project_id => 1, :id =>  'CookBook_documentation', :version => 2
567
    assert_response :success
568
    assert_template 'annotate'
569
570
    # Line 1
571
    assert_tag :tag => 'tr', :child => {
572
      :tag => 'th', :attributes => {:class => 'line-num'}, :content => '1', :sibling => {
573
        :tag => 'td', :attributes => {:class => 'author'}, :content => /John Smith/, :sibling => {
574
          :tag => 'td', :content => /h1\. CookBook documentation/
575
        }
576
      }
577
    }
578
579
    # Line 5
580
    assert_tag :tag => 'tr', :child => {
581
      :tag => 'th', :attributes => {:class => 'line-num'}, :content => '5', :sibling => {
582
        :tag => 'td', :attributes => {:class => 'author'}, :content => /redMine Admin/, :sibling => {
583
          :tag => 'td', :content => /Some updated \[\[documentation\]\] here/
584
        }
585
      }
586
    }
587
  end
588
589
  def test_annotate_with_invalid_version_should_respond_with_404
590
    get :annotate, :project_id => 1, :id => 'CookBook_documentation', :version => '99'
591
    assert_response 404
592
  end
593
594
  def test_get_rename
595
    @request.session[:user_id] = 2
596
    get :rename, :project_id => 1, :id => 'Another_page'
597
    assert_response :success
598
    assert_template 'rename'
599
    assert_tag 'option',
600
      :attributes => {:value => ''},
601
      :content => '',
602
      :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
603
    assert_no_tag 'option',
604
      :attributes => {:selected => 'selected'},
605
      :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
606
  end
607
608
  def test_get_rename_child_page
609
    @request.session[:user_id] = 2
610
    get :rename, :project_id => 1, :id => 'Child_1'
611
    assert_response :success
612
    assert_template 'rename'
613
    assert_tag 'option',
614
      :attributes => {:value => ''},
615
      :content => '',
616
      :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
617
    assert_tag 'option',
618
      :attributes => {:value => '2', :selected => 'selected'},
619
      :content => /Another page/,
620
      :parent => {
621
        :tag => 'select',
622
        :attributes => {:name => 'wiki_page[parent_id]'}
623
      }
624
  end
625
626
  def test_rename_with_redirect
627
    @request.session[:user_id] = 2
628
    post :rename, :project_id => 1, :id => 'Another_page',
629
                            :wiki_page => { :title => 'Another renamed page',
630
                                            :redirect_existing_links => 1 }
631
    assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
632
    wiki = Project.find(1).wiki
633
    # Check redirects
634
    assert_not_nil wiki.find_page('Another page')
635
    assert_nil wiki.find_page('Another page', :with_redirect => false)
636
  end
637
638
  def test_rename_without_redirect
639
    @request.session[:user_id] = 2
640
    post :rename, :project_id => 1, :id => 'Another_page',
641
                            :wiki_page => { :title => 'Another renamed page',
642
                                            :redirect_existing_links => "0" }
643
    assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
644
    wiki = Project.find(1).wiki
645
    # Check that there's no redirects
646
    assert_nil wiki.find_page('Another page')
647
  end
648
649
  def test_rename_with_parent_assignment
650
    @request.session[:user_id] = 2
651
    post :rename, :project_id => 1, :id => 'Another_page',
652
      :wiki_page => { :title => 'Another page', :redirect_existing_links => "0", :parent_id => '4' }
653
    assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
654
    assert_equal WikiPage.find(4), WikiPage.find_by_title('Another_page').parent
655
  end
656
657
  def test_rename_with_parent_unassignment
658
    @request.session[:user_id] = 2
659
    post :rename, :project_id => 1, :id => 'Child_1',
660
      :wiki_page => { :title => 'Child 1', :redirect_existing_links => "0", :parent_id => '' }
661
    assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Child_1'
662
    assert_nil WikiPage.find_by_title('Child_1').parent
663
  end
664
665
  def test_destroy_a_page_without_children_should_not_ask_confirmation
666
    @request.session[:user_id] = 2
667
    delete :destroy, :project_id => 1, :id => 'Child_2'
668
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
669
  end
670
671
  def test_destroy_parent_should_ask_confirmation
672
    @request.session[:user_id] = 2
673
    assert_no_difference('WikiPage.count') do
674
      delete :destroy, :project_id => 1, :id => 'Another_page'
675
    end
676
    assert_response :success
677
    assert_template 'destroy'
678
    assert_select 'form' do
679
      assert_select 'input[name=todo][value=nullify]'
680
      assert_select 'input[name=todo][value=destroy]'
681
      assert_select 'input[name=todo][value=reassign]'
682
    end
683
  end
684
685
  def test_destroy_parent_with_nullify_should_delete_parent_only
686
    @request.session[:user_id] = 2
687
    assert_difference('WikiPage.count', -1) do
688
      delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'nullify'
689
    end
690
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
691
    assert_nil WikiPage.find_by_id(2)
692
  end
693
694
  def test_destroy_parent_with_cascade_should_delete_descendants
695
    @request.session[:user_id] = 2
696
    assert_difference('WikiPage.count', -4) do
697
      delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'destroy'
698
    end
699
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
700
    assert_nil WikiPage.find_by_id(2)
701
    assert_nil WikiPage.find_by_id(5)
702
  end
703
704
  def test_destroy_parent_with_reassign
705
    @request.session[:user_id] = 2
706
    assert_difference('WikiPage.count', -1) do
707
      delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'reassign', :reassign_to_id => 1
708
    end
709
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
710
    assert_nil WikiPage.find_by_id(2)
711
    assert_equal WikiPage.find(1), WikiPage.find_by_id(5).parent
712
  end
713
714
  def test_destroy_version
715
    @request.session[:user_id] = 2
716
    assert_difference 'WikiContent::Version.count', -1 do
717
      assert_no_difference 'WikiContent.count' do
718
        assert_no_difference 'WikiPage.count' do
719
          delete :destroy_version, :project_id => 'ecookbook', :id => 'CookBook_documentation', :version => 2
720
          assert_redirected_to '/projects/ecookbook/wiki/CookBook_documentation/history'
721
        end
722
      end
723
    end
724
  end
725
726
  def test_index
727
    get :index, :project_id => 'ecookbook'
728
    assert_response :success
729
    assert_template 'index'
730
    pages = assigns(:pages)
731
    assert_not_nil pages
732
    assert_equal Project.find(1).wiki.pages.size, pages.size
733
    assert_equal pages.first.content.updated_on, pages.first.updated_on
734
735
    assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
736
                    :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/CookBook_documentation' },
737
                                              :content => 'CookBook documentation' },
738
                                :child => { :tag => 'ul',
739
                                            :child => { :tag => 'li',
740
                                                        :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
741
                                                                                 :content => 'Page with an inline image' } } } },
742
                    :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Another_page' },
743
                                                                       :content => 'Another page' } }
744
  end
745
746
  def test_index_should_include_atom_link
747
    get :index, :project_id => 'ecookbook'
748
    assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
749
  end
750
751
  def test_export_to_html
752
    @request.session[:user_id] = 2
753
    get :export, :project_id => 'ecookbook'
754
755
    assert_response :success
756
    assert_not_nil assigns(:pages)
757
    assert assigns(:pages).any?
758
    assert_equal "text/html", @response.content_type
759
760
    assert_select "a[name=?]", "CookBook_documentation"
761
    assert_select "a[name=?]", "Another_page"
762
    assert_select "a[name=?]", "Page_with_an_inline_image"
763
  end
764
765
  def test_export_to_pdf
766
    @request.session[:user_id] = 2
767
    get :export, :project_id => 'ecookbook', :format => 'pdf'
768
769
    assert_response :success
770
    assert_not_nil assigns(:pages)
771
    assert assigns(:pages).any?
772
    assert_equal 'application/pdf', @response.content_type
773
    assert_equal 'attachment; filename="ecookbook.pdf"', @response.headers['Content-Disposition']
774
    assert @response.body.starts_with?('%PDF')
775
  end
776
777
  def test_export_without_permission_should_be_denied
778
    @request.session[:user_id] = 2
779
    Role.find_by_name('Manager').remove_permission! :export_wiki_pages
780
    get :export, :project_id => 'ecookbook'
781
782
    assert_response 403
783
  end
784
785
  def test_date_index
786
    get :date_index, :project_id => 'ecookbook'
787
788
    assert_response :success
789
    assert_template 'date_index'
790
    assert_not_nil assigns(:pages)
791
    assert_not_nil assigns(:pages_by_date)
792
793
    assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
794
  end
795
796
  def test_not_found
797
    get :show, :project_id => 999
798
    assert_response 404
799
  end
800
801
  def test_protect_page
802
    page = WikiPage.find_by_wiki_id_and_title(1, 'Another_page')
803
    assert !page.protected?
804
    @request.session[:user_id] = 2
805
    post :protect, :project_id => 1, :id => page.title, :protected => '1'
806
    assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
807
    assert page.reload.protected?
808
  end
809
810
  def test_unprotect_page
811
    page = WikiPage.find_by_wiki_id_and_title(1, 'CookBook_documentation')
812
    assert page.protected?
813
    @request.session[:user_id] = 2
814
    post :protect, :project_id => 1, :id => page.title, :protected => '0'
815
    assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'CookBook_documentation'
816
    assert !page.reload.protected?
817
  end
818
819
  def test_show_page_with_edit_link
820
    @request.session[:user_id] = 2
821
    get :show, :project_id => 1
822
    assert_response :success
823
    assert_template 'show'
824
    assert_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
825
  end
826
827
  def test_show_page_without_edit_link
828
    @request.session[:user_id] = 4
829
    get :show, :project_id => 1
830
    assert_response :success
831
    assert_template 'show'
832
    assert_no_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
833
  end
834
835
  def test_show_pdf
836
    @request.session[:user_id] = 2
837
    get :show, :project_id => 1, :format => 'pdf'
838
    assert_response :success
839
    assert_not_nil assigns(:page)
840
    assert_equal 'application/pdf', @response.content_type
841
    assert_equal 'attachment; filename="CookBook_documentation.pdf"',
842
                  @response.headers['Content-Disposition']
843
  end
844
845
  def test_show_html
846
    @request.session[:user_id] = 2
847
    get :show, :project_id => 1, :format => 'html'
848
    assert_response :success
849
    assert_not_nil assigns(:page)
850
    assert_equal 'text/html', @response.content_type
851
    assert_equal 'attachment; filename="CookBook_documentation.html"',
852
                  @response.headers['Content-Disposition']
853
    assert_tag 'h1', :content => 'CookBook documentation'
854
  end
855
856
  def test_show_versioned_html
857
    @request.session[:user_id] = 2
858
    get :show, :project_id => 1, :format => 'html', :version => 2
859
    assert_response :success
860
    assert_not_nil assigns(:content)
861
    assert_equal 2, assigns(:content).version
862
    assert_equal 'text/html', @response.content_type
863
    assert_equal 'attachment; filename="CookBook_documentation.html"',
864
                  @response.headers['Content-Disposition']
865
    assert_tag 'h1', :content => 'CookBook documentation'
866
  end
867
868
  def test_show_txt
869
    @request.session[:user_id] = 2
870
    get :show, :project_id => 1, :format => 'txt'
871
    assert_response :success
872
    assert_not_nil assigns(:page)
873
    assert_equal 'text/plain', @response.content_type
874
    assert_equal 'attachment; filename="CookBook_documentation.txt"',
875
                  @response.headers['Content-Disposition']
876
    assert_include 'h1. CookBook documentation', @response.body
877
  end
878
879
  def test_show_versioned_txt
880
    @request.session[:user_id] = 2
881
    get :show, :project_id => 1, :format => 'txt', :version => 2
882
    assert_response :success
883
    assert_not_nil assigns(:content)
884
    assert_equal 2, assigns(:content).version
885
    assert_equal 'text/plain', @response.content_type
886
    assert_equal 'attachment; filename="CookBook_documentation.txt"',
887
                  @response.headers['Content-Disposition']
888
    assert_include 'h1. CookBook documentation', @response.body
889
  end
890
891
  def test_edit_unprotected_page
892
    # Non members can edit unprotected wiki pages
893
    @request.session[:user_id] = 4
894
    get :edit, :project_id => 1, :id => 'Another_page'
895
    assert_response :success
896
    assert_template 'edit'
897
  end
898
899
  def test_edit_protected_page_by_nonmember
900
    # Non members can't edit protected wiki pages
901
    @request.session[:user_id] = 4
902
    get :edit, :project_id => 1, :id => 'CookBook_documentation'
903
    assert_response 403
904
  end
905
906
  def test_edit_protected_page_by_member
907
    @request.session[:user_id] = 2
908
    get :edit, :project_id => 1, :id => 'CookBook_documentation'
909
    assert_response :success
910
    assert_template 'edit'
911
  end
912
913
  def test_history_of_non_existing_page_should_return_404
914
    get :history, :project_id => 1, :id => 'Unknown_page'
915
    assert_response 404
916
  end
917
918
  def test_add_attachment
919
    @request.session[:user_id] = 2
920
    assert_difference 'Attachment.count' do
921
      post :add_attachment, :project_id => 1, :id => 'CookBook_documentation',
922
        :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
923
    end
924
    attachment = Attachment.first(:order => 'id DESC')
925
    assert_equal Wiki.find(1).find_page('CookBook_documentation'), attachment.container
926
  end
927
end