tasks_controller_test.rb
9.05 KB
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
require File.dirname(__FILE__) + '/../test_helper'
require 'tasks_controller'
class TasksController; def rescue_action(e) raise e end; end
class TasksControllerTest < Test::Unit::TestCase
  noosfero_test :profile => 'testuser' 
  def setup
    @controller = TasksController.new
    @request    = ActionController::TestRequest.new
    @request.stubs(:ssl?).returns(true)
    @response   = ActionController::TestResponse.new
    self.profile = create_user('testuser').person
    @controller.stubs(:profile).returns(profile)
    login_as 'testuser'
  end
  attr_accessor :profile
  def test_local_files_reference
    assert_local_files_reference
  end
  
  def test_valid_xhtml
    assert_valid_xhtml
  end
  
  should 'list pending tasks' do
    get :index
    assert_response :success
    assert_template 'index'
    assert_kind_of Array, assigns(:tasks)
  end
  should 'list processed tasks' do
    get :processed
    assert_response :success
    assert_template 'processed'
    assert_kind_of Array, assigns(:tasks)
  end
  should 'be able to finish a task' do
    t = profile.tasks.build; t.save!
    post :close, :decision => 'finish', :id => t.id
    assert_redirected_to :action => 'index'
    t.reload
    ok('task should be finished') { t.status == Task::Status::FINISHED }
  end
  should 'be able to cancel a task' do
    t = profile.tasks.build; t.save!
    post :close, :decision => 'cancel', :id => t.id
    assert_redirected_to :action => 'index'
    t.reload
    ok('task should be cancelled') { t.status == Task::Status::CANCELLED }
  end
  should 'affiliate roles to user after finish add member task' do
    t = AddMember.create!(:person => profile, :organization => profile)
    count = profile.members.size
    post :close, :decision => 'finish', :id => t.id
    profile = Profile.find(@profile.id)
    assert_equal count + 1, profile.members.size
  end
  should 'display custom form to add members task' do
    t = AddMember.create!(:person => profile, :organization => profile)
    get :index, :profile => profile.identifier
    assert_tag :tag => 'form', :attributes => { :action => "/myprofile/#{profile.identifier}/tasks/close/#{t.id}" }
  end
  should 'display member role checked if target has members' do
    profile.affiliate(profile, Profile::Roles.admin(profile.environment.id))
    assert_equal 1, profile.members.size
    t = AddMember.create!(:person => profile, :organization => profile)
    get :index, :profile => profile.identifier
    assert_tag :tag => 'input', :attributes => { :name => 'task[roles][]', :checked => 'checked', :value => Profile::Roles.member(profile.environment.id).id }
  end
  should 'display roles besides role member unchecked if target has members' do
    profile.affiliate(profile, Profile::Roles.admin(profile.environment.id))
    assert_equal 1, profile.members.size
    t = AddMember.create!(:person => profile, :organization => profile)
    get :index, :profile => profile.identifier
    Role.find(:all).select{ |r| r.has_kind?('Profile') and r.id != Profile::Roles.member(profile.environment.id).id }.each do |i|
      assert_no_tag :tag => 'input', :attributes => { :name => 'task[roles][]', :checked => 'checked', :value => i.id }
    end
  end
  should 'display all roles checked if target has no members' do
    assert_equal 0, profile.members.size
    t = AddMember.create!(:person => profile, :organization => profile)
    get :index, :profile => profile.identifier
    Role.find(:all).select{ |r| r.has_kind?('Profile') }.each do |i|
      assert_tag :tag => 'input', :attributes => { :name => 'task[roles][]', :checked => 'checked', :value => i.id }
    end
  end
  should 'display a create ticket form' do
    get :new, :profile => profile.identifier
    assert_template 'new'
  end
  should 'add a hidden field with target_id when informed in the URL' do
    friend = create_user('myfriend').person
    profile.add_friend(friend)
    get :new, :profile => profile.identifier, :target_id => friend.id.to_s
    assert_tag :tag => 'input', :attributes => { :type => 'hidden', :name => 'ticket[target_id]', :value => friend.id }
  end
  should 'select friend from list when not already informed' do
    get :new, :profile => profile.identifier
    assert_tag :tag => 'select', :attributes => { :name =>  'ticket[target_id]' }
  end
  should 'create a ticket' do
    assert_difference Ticket, :count do
      post :new, :profile => profile.identifier, :ticket => {:title => 'test ticket'}
    end
  end
  should 'create a ticket with profile requestor' do
    post :new, :profile => profile.identifier, :ticket => {:title => 'new task'} 
    
    assert_equal profile, assigns(:ticket).requestor
  end
  should 'list tasks that this profile created' do
    task = Ticket.create!(:title => 'test', :requestor => profile)
    get :list_requested, :profile => profile.identifier
    assert_includes assigns(:tasks), task
  end
  should 'set target of ticket when creating it' do
     f = create_user('friend').person
     profile.add_friend f
     post :new, :profile => profile.identifier, :ticket => {:title => 'test ticket', :target_id => f.id, :target_type => 'Profile'}
     assert_response :redirect
     assert_equal f, assigns(:ticket).target
  end
  should 'create published article after finish approve article task' do
    PublishedArticle.destroy_all
    c = Community.create!(:name => 'test comm', :moderated_articles => false)
    @controller.stubs(:profile).returns(c)
    c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))
    article = profile.articles.create!(:name => 'something interesting', :body => 'ruby on rails')
    t = ApproveArticle.create!(:name => 'test name', :article => article, :target => c, :requestor => profile)
    post :close, :decision => 'finish', :id => t.id, :task => { :name => 'new_name'}
    assert_equal article, PublishedArticle.find(:first).reference_article
  end
  should 'create published article in folder after finish approve article task' do
    PublishedArticle.destroy_all
    c = Community.create!(:name => 'test comm', :moderated_articles => false)
    @controller.stubs(:profile).returns(c)
    folder = c.articles.create!(:name => 'test folder', :type => 'Folder')
    c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))
    article = profile.articles.create!(:name => 'something interesting', :body => 'ruby on rails')
    t = ApproveArticle.create!(:name => 'test name', :article => article, :target => c, :requestor => profile)
    post :close, :decision => 'finish', :id => t.id, :task => { :name => 'new_name', :article_parent_id => folder.id}
    assert_equal folder, PublishedArticle.find(:first).parent
  end
  should 'be highlighted if asked when approving a published article' do
    PublishedArticle.destroy_all
    c = Community.create!(:name => 'test comm', :moderated_articles => false)
    @controller.stubs(:profile).returns(c)
    folder = c.articles.create!(:name => 'test folder', :type => 'Folder')
    c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))
    article = profile.articles.create!(:name => 'something interesting', :body => 'ruby on rails')
    t = ApproveArticle.create!(:name => 'test name', :article => article, :target => c, :requestor => profile)
    post :close, :decision => 'finish', :id => t.id, :task => { :name => 'new_name', :article_parent_id => folder.id, :highlighted => true}
    assert_equal true, PublishedArticle.find(:first).highlighted
  end
  should 'create published article after choosing root folder on approve article task' do
    PublishedArticle.destroy_all
    c = Community.create!(:name => 'test comm', :moderated_articles => false)
    @controller.stubs(:profile).returns(c)
    c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))
    article = profile.articles.create!(:name => 'something interesting', :body => 'ruby on rails')
    t = ApproveArticle.create!(:name => 'test name', :article => article, :target => c, :requestor => profile)
    post :close, :decision => 'finish', :id => t.id, :task => { :name => 'new_name', :article_parent_id => ""}
    assert_not_nil PublishedArticle.find(:first)
  end
  should 'handle blank names for published articles' do
    c = Community.create!(:name => 'test comm')
    @controller.stubs(:profile).returns(c)
    c.affiliate(profile, Profile::Roles.all_roles(c.environment))
    person = create_user('test_user').person
    p_blog = Blog.create!(:profile => person, :name => 'Blog')
    c_blog1 = Blog.create!(:profile => c, :name => 'Blog')
    c_blog2 = Blog.new(:profile => c); c_blog2.name = 'blog2'; c_blog2.save!
    article = person.articles.create!(:name => 'test article', :parent => p_blog)
    a = ApproveArticle.create!(:article => article, :target => c, :requestor => person)
    assert_includes c.tasks, a
    assert_difference PublishedArticle, :count do
        post :close, {"commit"=>"Ok!", "id"=> a.id.to_s, "task"=>{"name"=>"", "closing_statment"=>"", "highlighted"=>"0", "article_parent_id"=>c_blog2.id.to_s}, "decision"=>"finish"}
    end
    assert p_article = PublishedArticle.find_by_reference_article_id(article.id)
    assert_includes c_blog2.children(true), p_article
  end
end