Plugin Tutorial » History » Version 51
Igor Zubkov, 2011-09-22 11:00
Add spaces hashes (part 2)
1 | 1 | Jean-Philippe Lang | h1. Plugin Tutorial |
---|---|---|---|
2 | 12 | Jean-Philippe Lang | |
3 | 20 | Jean-Philippe Lang | Note: To follow this tutorial, you need to run Redmine devel r1786 or higher. |
4 | |||
5 | 30 | Vinod Singh | {{>toc}} |
6 | 1 | Jean-Philippe Lang | |
7 | h2. Creating a new Plugin |
||
8 | 40 | Nick Peelman | |
9 | You may need to set the RAILS_ENV variable in order to use the command below: |
||
10 | 32 | Jiří Křivánek | |
11 | <pre> |
||
12 | $ export RAILS_ENV="production" |
||
13 | </pre> |
||
14 | |||
15 | 9 | Jean-Philippe Lang | Creating a new plugin can be done using the Redmine plugin generator. |
16 | Syntax for this generator is: |
||
17 | 1 | Jean-Philippe Lang | |
18 | 23 | Jean-Baptiste Barth | <pre>ruby script/generate redmine_plugin <plugin_name></pre> |
19 | 9 | Jean-Philippe Lang | |
20 | So open up a command prompt and "cd" to your redmine directory, then execute the following command: |
||
21 | |||
22 | 18 | Jean-Philippe Lang | % ruby script/generate redmine_plugin Polls |
23 | 1 | Jean-Philippe Lang | |
24 | 18 | Jean-Philippe Lang | The plugin structure is created in @vendor/plugins/redmine_polls@: |
25 | 1 | Jean-Philippe Lang | |
26 | <pre> |
||
27 | 18 | Jean-Philippe Lang | create vendor/plugins/redmine_polls/app/controllers |
28 | create vendor/plugins/redmine_polls/app/helpers |
||
29 | create vendor/plugins/redmine_polls/app/models |
||
30 | create vendor/plugins/redmine_polls/app/views |
||
31 | create vendor/plugins/redmine_polls/db/migrate |
||
32 | create vendor/plugins/redmine_polls/lib/tasks |
||
33 | create vendor/plugins/redmine_polls/assets/images |
||
34 | create vendor/plugins/redmine_polls/assets/javascripts |
||
35 | create vendor/plugins/redmine_polls/assets/stylesheets |
||
36 | create vendor/plugins/redmine_polls/lang |
||
37 | create vendor/plugins/redmine_polls/README |
||
38 | create vendor/plugins/redmine_polls/init.rb |
||
39 | create vendor/plugins/redmine_polls/lang/en.yml |
||
40 | 1 | Jean-Philippe Lang | </pre> |
41 | |||
42 | 18 | Jean-Philippe Lang | Edit @vendor/plugins/redmine_polls/init.rb@ to adjust plugin information (name, author, description and version): |
43 | 1 | Jean-Philippe Lang | |
44 | <pre><code class="ruby"> |
||
45 | require 'redmine' |
||
46 | |||
47 | 18 | Jean-Philippe Lang | Redmine::Plugin.register :redmine_polls do |
48 | name 'Polls plugin' |
||
49 | 1 | Jean-Philippe Lang | author 'John Smith' |
50 | 18 | Jean-Philippe Lang | description 'A plugin for managing polls' |
51 | 1 | Jean-Philippe Lang | version '0.0.1' |
52 | end |
||
53 | </code></pre> |
||
54 | |||
55 | 27 | Eduardo Yáñez Parareda | Then restart the application and point your browser to http://localhost:3000/admin/plugins. |
56 | 1 | Jean-Philippe Lang | After logging in, you should see your new plugin in the plugins list: |
57 | 4 | Jean-Philippe Lang | |
58 | 29 | Vinod Singh | !plugins_list1.png! |
59 | 1 | Jean-Philippe Lang | |
60 | 13 | Jean-Philippe Lang | h2. Generating a model |
61 | |||
62 | 44 | John Yani | For now plugin doesn't store anything. Let's create a simple Poll model for our plugin. Syntax is: |
63 | 1 | Jean-Philippe Lang | |
64 | 44 | John Yani | <pre> |
65 | ruby script/generate redmine_plugin_model <plugin_name> <model_name> [<fields>] |
||
66 | </pre> |
||
67 | |||
68 | So, go to the command prompt and run: |
||
69 | |||
70 | <pre> |
||
71 | 1 | Jean-Philippe Lang | ruby script/generate redmine_plugin_model polls poll question:string yes:integer no:integer |
72 | 44 | John Yani | </pre> |
73 | 14 | Jean-Philippe Lang | |
74 | 19 | Jean-Philippe Lang | This creates the Poll model and the corresponding migration file. |
75 | 1 | Jean-Philippe Lang | |
76 | 28 | John Fisher | *Please note you may have to rename your migration.* Timestamped migrations are not supported by the actual Redmine plugin engine (Engines). If your migrations are named with a timestamp, rename it using "001", "002", etc. instead. |
77 | |||
78 | 49 | Igor Zubkov | <pre>mv vendor/plugins/redmine_polls/db/migrate/20091009211553_create_polls.rb vendor/plugins/redmine_polls/db/migrate/001_create_polls.rb</pre> |
79 | 28 | John Fisher | |
80 | If you have already created a database table record in plugin_schema_info with the timestamp version number, you will have to change it to reflect your new version number, or the migration will hang. |
||
81 | |||
82 | 21 | Jean-Baptiste Barth | |
83 | 14 | Jean-Philippe Lang | Migrate the database using the following command: |
84 | |||
85 | rake db:migrate_plugins |
||
86 | |||
87 | Note that each plugin has its own set of migrations. |
||
88 | |||
89 | 24 | Eric Davis | Lets add some Polls in the console so we have something to work with. The console is where you an interactively work and examine the Redmine environment and is very informative to play around in. But for now we just need create two Poll objects |
90 | |||
91 | <pre> |
||
92 | script/console |
||
93 | >> Poll.create(:question => "Can you see this poll ?") |
||
94 | >> Poll.create(:question => "And can you see this other poll ?") |
||
95 | >> exit |
||
96 | </pre> |
||
97 | |||
98 | 26 | Eric Davis | Edit @vendor/plugins/redmine_polls/app/models/poll.rb@ in your plugin directory to add a #vote method that will be invoked from our controller: |
99 | 15 | Jean-Philippe Lang | |
100 | <pre><code class="ruby"> |
||
101 | 19 | Jean-Philippe Lang | class Poll < ActiveRecord::Base |
102 | 15 | Jean-Philippe Lang | def vote(answer) |
103 | increment(answer == 'yes' ? :yes : :no) |
||
104 | end |
||
105 | end |
||
106 | </code></pre> |
||
107 | |||
108 | 1 | Jean-Philippe Lang | h2. Generating a controller |
109 | |||
110 | For now, the plugin doesn't do anything. So let's create a controller for our plugin. |
||
111 | 9 | Jean-Philippe Lang | We can use the plugin controller generator for that. Syntax is: |
112 | |||
113 | 23 | Jean-Baptiste Barth | <pre>ruby script/generate redmine_plugin_controller <plugin_name> <controller_name> [<actions>]</pre> |
114 | 9 | Jean-Philippe Lang | |
115 | So go back to the command prompt and run: |
||
116 | 3 | Jean-Philippe Lang | |
117 | <pre> |
||
118 | 18 | Jean-Philippe Lang | % ruby script/generate redmine_plugin_controller Polls polls index vote |
119 | 3 | Jean-Philippe Lang | exists app/controllers/ |
120 | exists app/helpers/ |
||
121 | 18 | Jean-Philippe Lang | create app/views/polls |
122 | 3 | Jean-Philippe Lang | create test/functional/ |
123 | 18 | Jean-Philippe Lang | create app/controllers/polls_controller.rb |
124 | create test/functional/polls_controller_test.rb |
||
125 | create app/helpers/polls_helper.rb |
||
126 | create app/views/polls/index.html.erb |
||
127 | create app/views/polls/vote.html.erb |
||
128 | 3 | Jean-Philippe Lang | </pre> |
129 | |||
130 | 18 | Jean-Philippe Lang | A controller @PollsController@ with 2 actions (@#index@ and @#vote@) is created. |
131 | 3 | Jean-Philippe Lang | |
132 | 26 | Eric Davis | Edit @vendor/plugins/redmine_polls/app/controllers/polls_controller.rb@ in @redmine_polls@ directory to implement these 2 actions. |
133 | 3 | Jean-Philippe Lang | |
134 | <pre><code class="ruby"> |
||
135 | 18 | Jean-Philippe Lang | class PollsController < ApplicationController |
136 | 1 | Jean-Philippe Lang | unloadable |
137 | |||
138 | 7 | Jean-Philippe Lang | def index |
139 | 19 | Jean-Philippe Lang | @polls = Poll.find(:all) |
140 | 3 | Jean-Philippe Lang | end |
141 | 7 | Jean-Philippe Lang | |
142 | 19 | Jean-Philippe Lang | def vote |
143 | 1 | Jean-Philippe Lang | poll = Poll.find(params[:id]) |
144 | 21 | Jean-Baptiste Barth | poll.vote(params[:answer]) |
145 | 25 | Eric Davis | if poll.save |
146 | flash[:notice] = 'Vote saved.' |
||
147 | redirect_to :action => 'index' |
||
148 | end |
||
149 | 3 | Jean-Philippe Lang | end |
150 | end |
||
151 | 1 | Jean-Philippe Lang | </code></pre> |
152 | 5 | Jean-Philippe Lang | |
153 | 26 | Eric Davis | Then edit @vendor/plugins/redmine_polls/app/views/polls/index.html.erb@ that will display existing polls: |
154 | 3 | Jean-Philippe Lang | |
155 | |||
156 | <pre> |
||
157 | 18 | Jean-Philippe Lang | <h2>Polls</h2> |
158 | 3 | Jean-Philippe Lang | |
159 | 19 | Jean-Philippe Lang | <% @polls.each do |poll| %> |
160 | 3 | Jean-Philippe Lang | <p> |
161 | 19 | Jean-Philippe Lang | <%= poll[:question] %>? |
162 | 50 | Igor Zubkov | <%= link_to 'Yes', { :action => 'vote', :id => poll[:id], :answer => 'yes' }, :method => :post %> (<%= poll[:yes] %>) / |
163 | <%= link_to 'No', { :action => 'vote', :id => poll[:id], :answer => 'no' }, :method => :post %> (<%= poll[:no] %>) |
||
164 | 3 | Jean-Philippe Lang | </p> |
165 | <% end %> |
||
166 | </pre> |
||
167 | |||
168 | 26 | Eric Davis | You can remove @vendor/plugins/redmine_polls/app/views/polls/vote.html.erb@ since no rendering is done by the corresponding action. |
169 | 3 | Jean-Philippe Lang | |
170 | 18 | Jean-Philippe Lang | Now, restart the application and point your browser to http://localhost:3000/polls. |
171 | You should see the 2 polls and you should be able to vote for them: |
||
172 | 4 | Jean-Philippe Lang | |
173 | 29 | Vinod Singh | !pools1.png! |
174 | 4 | Jean-Philippe Lang | |
175 | 19 | Jean-Philippe Lang | Note that poll results are reset on each request if you don't run the application in production mode, since our poll "model" is stored in a class variable in this example. |
176 | 4 | Jean-Philippe Lang | |
177 | 37 | Randy Syring | h2. Translations |
178 | |||
179 | 38 | Randy Syring | The location of *.yml translation files is dependent on the version of Redmine that is being run: |
180 | |||
181 | |_. Version |_. Path| |
||
182 | | < 0.9 | @.../redmine_polls/lang@ | |
||
183 | | >= 0.9 | @.../redmine_polls/config/locales@ | |
||
184 | |||
185 | If you want your plugin to work in both versions, you will need to have the same translation file in both locations. |
||
186 | 37 | Randy Syring | |
187 | 4 | Jean-Philippe Lang | h2. Extending menus |
188 | |||
189 | 18 | Jean-Philippe Lang | Our controller works fine but users have to know the url to see the polls. Using the Redmine plugin API, you can extend standard menus. |
190 | 4 | Jean-Philippe Lang | So let's add a new item to the application menu. |
191 | |||
192 | h3. Extending the application menu |
||
193 | |||
194 | 26 | Eric Davis | Edit @vendor/plugins/redmine_polls/init.rb@ at the root of your plugin directory to add the following line at the end of the plugin registration block: |
195 | 4 | Jean-Philippe Lang | |
196 | <pre><code class="ruby"> |
||
197 | 18 | Jean-Philippe Lang | Redmine::Plugin.register :redmine_polls do |
198 | 4 | Jean-Philippe Lang | [...] |
199 | |||
200 | 18 | Jean-Philippe Lang | menu :application_menu, :polls, { :controller => 'polls', :action => 'index' }, :caption => 'Polls' |
201 | 4 | Jean-Philippe Lang | end |
202 | </code></pre> |
||
203 | |||
204 | Syntax is: |
||
205 | |||
206 | menu(menu_name, item_name, url, options={}) |
||
207 | |||
208 | 42 | Mischa The Evil | There are five menus that you can extend: |
209 | 4 | Jean-Philippe Lang | |
210 | * @:top_menu@ - the top left menu |
||
211 | * @:account_menu@ - the top right menu with sign in/sign out links |
||
212 | * @:application_menu@ - the main menu displayed when the user is not inside a project |
||
213 | * @:project_menu@ - the main menu displayed when the user is inside a project |
||
214 | 41 | Nick Peelman | * @:admin_menu@ - the menu displayed on the Administration page (can only insert after Settings, before Plugins) |
215 | 4 | Jean-Philippe Lang | |
216 | Available options are: |
||
217 | |||
218 | * @:param@ - the parameter key that is used for the project id (default is @:id@) |
||
219 | * @:if@ - a Proc that is called before rendering the item, the item is displayed only if it returns true |
||
220 | * @:caption@ - the menu caption that can be: |
||
221 | |||
222 | * a localized string Symbol |
||
223 | * a String |
||
224 | * a Proc that can take the project as argument |
||
225 | |||
226 | * @:before@, @:after@ - specify where the menu item should be inserted (eg. @:after => :activity@) |
||
227 | 36 | Jérémie Delaitre | * @:first@, @:last@ - if set to true, the item will stay at the beginning/end of the menu (eg. @:last => true@) |
228 | * @:html@ - a hash of html options that are passed to @link_to@ when rendering the menu item |
||
229 | 4 | Jean-Philippe Lang | |
230 | In our example, we've added an item to the application menu which is emtpy by default. |
||
231 | Restart the application and go to http://localhost:3000: |
||
232 | |||
233 | 29 | Vinod Singh | !application_menu.png! |
234 | 4 | Jean-Philippe Lang | |
235 | 18 | Jean-Philippe Lang | Now you can access the polls by clicking the Polls tab from the welcome screen. |
236 | 4 | Jean-Philippe Lang | |
237 | h3. Extending the project menu |
||
238 | |||
239 | 19 | Jean-Philippe Lang | Now, let's consider that the polls are defined at project level (even if it's not the case in our example poll model). So we would like to add the Polls tab to the project menu instead. |
240 | 6 | Jean-Philippe Lang | Open @init.rb@ and replace the line that was added just before with these 2 lines: |
241 | |||
242 | <pre><code class="ruby"> |
||
243 | 18 | Jean-Philippe Lang | Redmine::Plugin.register :redmine_polls do |
244 | 6 | Jean-Philippe Lang | [...] |
245 | |||
246 | 51 | Igor Zubkov | permission :polls, { :polls => [:index, :vote] }, :public => true |
247 | 18 | Jean-Philippe Lang | menu :project_menu, :polls, { :controller => 'polls', :action => 'index' }, :caption => 'Polls', :after => :activity, :param => :project_id |
248 | 6 | Jean-Philippe Lang | end |
249 | </code></pre> |
||
250 | |||
251 | 18 | Jean-Philippe Lang | The second line adds our Polls tab to the project menu, just after the activity tab. |
252 | The first line is required and declares that our 2 actions from @PollsController@ are public. We'll come back later to explain this with more details. |
||
253 | 6 | Jean-Philippe Lang | |
254 | Restart the application again and go to one of your projects: |
||
255 | |||
256 | 39 | Ric Turley | !http://www.redmine.org/attachments/3773/project_menu.png! |
257 | 6 | Jean-Philippe Lang | |
258 | 18 | Jean-Philippe Lang | If you click the Polls tab, you should notice that the project menu is no longer displayed. |
259 | 6 | Jean-Philippe Lang | To make the project menu visible, you have to initialize the controller's instance variable @@project@. |
260 | |||
261 | 18 | Jean-Philippe Lang | Edit your PollsController to do so: |
262 | 6 | Jean-Philippe Lang | |
263 | <pre><code class="ruby"> |
||
264 | def index |
||
265 | @project = Project.find(params[:project_id]) |
||
266 | 19 | Jean-Philippe Lang | @polls = Poll.find(:all) # @project.polls |
267 | 6 | Jean-Philippe Lang | end |
268 | </code></pre> |
||
269 | |||
270 | The project id is available in the @:project_id@ param because of the @:param => :project_id@ option in the menu item declaration above. |
||
271 | |||
272 | 18 | Jean-Philippe Lang | Now, you should see the project menu when viewing the polls: |
273 | 6 | Jean-Philippe Lang | |
274 | 39 | Ric Turley | !http://www.redmine.org/attachments/3774/project_menu_pools.png! |
275 | 4 | Jean-Philippe Lang | |
276 | h2. Adding new permissions |
||
277 | |||
278 | 18 | Jean-Philippe Lang | For now, anyone can vote for polls. Let's make it more configurable by changing the permission declaration. |
279 | We're going to declare 2 project based permissions, one for viewing the polls and an other one for voting. These permissions are no longer public (@:public => true@ option is removed). |
||
280 | 10 | Jean-Philippe Lang | |
281 | 26 | Eric Davis | Edit @vendor/plugins/redmine_polls/init.rb@ to replace the previous permission declaration with these 2 lines: |
282 | 10 | Jean-Philippe Lang | |
283 | <pre><code class="ruby"> |
||
284 | 20 | Jean-Philippe Lang | |
285 | 18 | Jean-Philippe Lang | permission :view_polls, :polls => :index |
286 | permission :vote_polls, :polls => :vote |
||
287 | 1 | Jean-Philippe Lang | </code></pre> |
288 | 14 | Jean-Philippe Lang | |
289 | 10 | Jean-Philippe Lang | |
290 | Restart the application and go to http://localhost:3000/roles/report: |
||
291 | |||
292 | 29 | Vinod Singh | !permissions1.png! |
293 | 10 | Jean-Philippe Lang | |
294 | You're now able to give these permissions to your existing roles. |
||
295 | |||
296 | 18 | Jean-Philippe Lang | Of course, some code needs to be added to the PollsController so that actions are actually protected according to the permissions of the current user. |
297 | 10 | Jean-Philippe Lang | For this, we just need to append the @:authorize@ filter and make sure that the @project instance variable is properly set before calling this filter. |
298 | |||
299 | Here is how it would look like for the @#index@ action: |
||
300 | |||
301 | 1 | Jean-Philippe Lang | <pre><code class="ruby"> |
302 | 18 | Jean-Philippe Lang | class PollsController < ApplicationController |
303 | 10 | Jean-Philippe Lang | unloadable |
304 | |||
305 | before_filter :find_project, :authorize, :only => :index |
||
306 | |||
307 | [...] |
||
308 | |||
309 | def index |
||
310 | 19 | Jean-Philippe Lang | @polls = Poll.find(:all) # @project.polls |
311 | 10 | Jean-Philippe Lang | end |
312 | |||
313 | [...] |
||
314 | |||
315 | private |
||
316 | |||
317 | def find_project |
||
318 | # @project variable must be set before calling the authorize filter |
||
319 | @project = Project.find(params[:project_id]) |
||
320 | end |
||
321 | end |
||
322 | </code></pre> |
||
323 | |||
324 | 18 | Jean-Philippe Lang | Retrieving the current project before the @#vote@ action could be done using a similar way. |
325 | 4 | Jean-Philippe Lang | After this, viewing and voting polls will be only available to admin users or users that have the appropriate role on the project. |
326 | 31 | Markus Bockman | |
327 | 1 | Jean-Philippe Lang | If you want to display the symbols of your permissions in a multilangual way, you need to add the necessary text labels in a language file. |
328 | 37 | Randy Syring | Simply create an *.yml file in the correct translation directory for your Redmine version and fill it with labels like this: |
329 | 31 | Markus Bockman | |
330 | <pre><code class="ruby"> |
||
331 | |||
332 | permission_view_polls: View Polls |
||
333 | permission_vote_polls: Vote Polls |
||
334 | |||
335 | </code></pre> |
||
336 | |||
337 | In this example the created file is known as en.yml, but all other supported language files are also possible too. |
||
338 | As you can see on the example above, the labels consists of the permission symbols @:view_polls@ and @:vote_polls@ with an additional @permission_@ added at the front. |
||
339 | |||
340 | Restart your application and point the permission section. |
||
341 | |||
342 | 4 | Jean-Philippe Lang | h2. Creating a project module |
343 | |||
344 | 19 | Jean-Philippe Lang | For now, the poll functionality is added to all your projects. But you way want to enable polls for some projects only. |
345 | 26 | Eric Davis | So, let's create a 'Polls' project module. This is done by wrapping the permissions declaration inside a call to @#project_module@. |
346 | 11 | Jean-Philippe Lang | |
347 | Edit @init.rb@ and change the permissions declaration: |
||
348 | |||
349 | <pre><code class="ruby"> |
||
350 | 18 | Jean-Philippe Lang | project_module :polls do |
351 | permission :view_polls, :polls => :index |
||
352 | permission :vote_polls, :polls => :vote |
||
353 | 11 | Jean-Philippe Lang | end |
354 | </code></pre> |
||
355 | |||
356 | Restart the application and go to one of your project settings. |
||
357 | 18 | Jean-Philippe Lang | Click on the Modules tab. You should see the Polls module at the end of the modules list (disabled by default): |
358 | 11 | Jean-Philippe Lang | |
359 | 29 | Vinod Singh | !modules.png! |
360 | 11 | Jean-Philippe Lang | |
361 | 18 | Jean-Philippe Lang | You can now enable/disable polls at project level. |
362 | 11 | Jean-Philippe Lang | |
363 | h2. Improving the plugin views |
||
364 | |||
365 | 16 | Jean-Philippe Lang | h3. Adding stylesheets |
366 | |||
367 | Let's start by adding a stylesheet to our plugin views. |
||
368 | 26 | Eric Davis | Create a file named @voting.css@ in the @vendor/plugins/redmine_polls/assets/stylesheets@ directory: |
369 | 16 | Jean-Philippe Lang | |
370 | <pre> |
||
371 | a.vote { font-size: 120%; } |
||
372 | a.vote.yes { color: green; } |
||
373 | a.vote.no { color: red; } |
||
374 | </pre> |
||
375 | |||
376 | 18 | Jean-Philippe Lang | When starting the application, plugin assets are automatically copied to @public/plugin_assets/redmine_polls/@ by Rails Engines to make them available through your web server. So any change to your plugin stylesheets or javascripts needs an application restart. |
377 | 16 | Jean-Philippe Lang | |
378 | 26 | Eric Davis | Then, append the following lines at the end of @vendor/plugins/redmine_polls/app/views/polls/index.html.erb@ so that your stylesheet get included in the page header by Redmine: |
379 | 16 | Jean-Philippe Lang | |
380 | <pre> |
||
381 | <% content_for :header_tags do %> |
||
382 | 18 | Jean-Philippe Lang | <%= stylesheet_link_tag 'voting', :plugin => 'redmine_polls' %> |
383 | 16 | Jean-Philippe Lang | <% end %> |
384 | </pre> |
||
385 | |||
386 | 18 | Jean-Philippe Lang | Note that the @:plugin => 'redmine_polls'@ option is required when calling the @stylesheet_link_tag@ helper. |
387 | 16 | Jean-Philippe Lang | |
388 | Javascripts can be included in plugin views using the @javascript_include_tag@ helper in the same way. |
||
389 | |||
390 | h3. Setting page title |
||
391 | |||
392 | You can set the HTML title from inside your views by using the @html_title@ helper. |
||
393 | Example: |
||
394 | |||
395 | 18 | Jean-Philippe Lang | <% html_title "Polls" -%> |
396 | 34 | Tom Bostelmann | |
397 | |||
398 | h2. Testing your plugin |
||
399 | |||
400 | h3. test/test_helper.rb: |
||
401 | |||
402 | Here are the contents of my test helper file: |
||
403 | |||
404 | <pre> |
||
405 | require File.expand_path(File.dirname(__FILE__) + '/../../../../test/test_helper') |
||
406 | </pre> |
||
407 | |||
408 | h3. Sample test: |
||
409 | |||
410 | Contents of requirements_controller_test.rb: |
||
411 | |||
412 | <pre> |
||
413 | require File.dirname(__FILE__) + '/../test_helper' |
||
414 | require 'requirements_controller' |
||
415 | |||
416 | class RequirementsControllerTest < ActionController::TestCase |
||
417 | fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details, |
||
418 | :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages, |
||
419 | :attachments, :custom_fields, :custom_values, :time_entries |
||
420 | |||
421 | def setup |
||
422 | @skill = Skill.new(:skill_name => 'Java') |
||
423 | @project = Project.find(1) |
||
424 | @request = ActionController::TestRequest.new |
||
425 | @response = ActionController::TestResponse.new |
||
426 | User.current = nil |
||
427 | end |
||
428 | |||
429 | def test_routing |
||
430 | assert_routing( |
||
431 | {:method => :get, :path => '/requirements'}, |
||
432 | :controller => 'requirements', :action => 'index' |
||
433 | ) |
||
434 | end |
||
435 | </pre> |
||
436 | |||
437 | h3. Initialize Test DB: |
||
438 | |||
439 | I found it easiest to initialize the test db directly with the following rake call: |
||
440 | |||
441 | <pre> |
||
442 | 43 | David Fischer | rake db:drop db:create db:migrate db:migrate_plugins redmine:load_default_data RAILS_ENV=test |
443 | 34 | Tom Bostelmann | </pre> |
444 | |||
445 | h3. Run test: |
||
446 | |||
447 | 48 | Igor Zubkov | To execute the requirements_controller_test.rb I used the following command: |
448 | 34 | Tom Bostelmann | |
449 | <pre> |
||
450 | rake test:engines:all PLUGIN=redmine_requirements |
||
451 | </pre> |
||
452 | 35 | Tom Bostelmann | |
453 | 47 | Mo Morsi | h3. Testing with permissions |
454 | 35 | Tom Bostelmann | |
455 | If your plugin requires membership to a project, add the following to the beginning of your functional tests: |
||
456 | |||
457 | 1 | Jean-Philippe Lang | <pre> |
458 | 47 | Mo Morsi | def test_index |
459 | @request.session[:user_id] = 2 |
||
460 | ... |
||
461 | 1 | Jean-Philippe Lang | end |
462 | 47 | Mo Morsi | </pre> |
463 | 1 | Jean-Philippe Lang | |
464 | 47 | Mo Morsi | If your plugin requires a specific permission, you can add that to a user role like so (lookup which role is appropriate for the user in the fixtures): |
465 | |||
466 | <pre> |
||
467 | 1 | Jean-Philippe Lang | def test_index |
468 | 47 | Mo Morsi | Role.find(1).add_permission! :my_permission |
469 | ... |
||
470 | 1 | Jean-Philippe Lang | end |
471 | </pre> |
||
472 | 35 | Tom Bostelmann | |
473 | 47 | Mo Morsi | |
474 | You may enable/disable a specific module like so: |
||
475 | |||
476 | <pre> |
||
477 | def test_index |
||
478 | Project.find(1).enabled_module_names = [:mymodule] |
||
479 | ... |
||
480 | end |
||
481 | </pre> |