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 / a0 / a0cb5d986928907b89ed6e9fd9c7a2000dd01933.svn-base @ 1297:0a574315af3e

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