You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timelog_controller_test.rb 45KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  1. # -*- coding: utf-8 -*-
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2017 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class TimelogControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :enabled_modules, :roles, :members,
  21. :member_roles, :issues, :time_entries, :users,
  22. :trackers, :enumerations, :issue_statuses,
  23. :custom_fields, :custom_values,
  24. :projects_trackers, :custom_fields_trackers,
  25. :custom_fields_projects, :issue_categories
  26. include Redmine::I18n
  27. def setup
  28. super
  29. Setting.default_language = 'en'
  30. end
  31. def test_new
  32. @request.session[:user_id] = 3
  33. get :new
  34. assert_response :success
  35. assert_select 'input[name=?][type=hidden]', 'project_id', 0
  36. assert_select 'input[name=?][type=hidden]', 'issue_id', 0
  37. assert_select 'span[id=?]', 'time_entry_issue'
  38. assert_select 'select[name=?]', 'time_entry[project_id]' do
  39. # blank option for project
  40. assert_select 'option[value=""]'
  41. end
  42. end
  43. def test_new_with_project_id
  44. @request.session[:user_id] = 3
  45. get :new, :params => {:project_id => 1}
  46. assert_response :success
  47. assert_select 'input[name=?][type=hidden]', 'project_id'
  48. assert_select 'input[name=?][type=hidden]', 'issue_id', 0
  49. assert_select 'select[name=?]', 'time_entry[project_id]', 0
  50. end
  51. def test_new_with_issue_id
  52. @request.session[:user_id] = 3
  53. get :new, :params => {:issue_id => 2}
  54. assert_response :success
  55. assert_select 'input[name=?][type=hidden]', 'project_id', 0
  56. assert_select 'input[name=?][type=hidden]', 'issue_id'
  57. assert_select 'a[href=?]', '/issues/2', :text => /Feature request #2/
  58. assert_select 'select[name=?]', 'time_entry[project_id]', 0
  59. end
  60. def test_new_without_project_should_prefill_the_form
  61. @request.session[:user_id] = 3
  62. get :new, :params => {:time_entry => {:project_id => '1'}}
  63. assert_response :success
  64. assert_select 'select[name=?]', 'time_entry[project_id]' do
  65. assert_select 'option[value="1"][selected=selected]'
  66. end
  67. end
  68. def test_new_without_project_should_deny_without_permission
  69. Role.all.each {|role| role.remove_permission! :log_time}
  70. @request.session[:user_id] = 3
  71. get :new
  72. assert_response 403
  73. end
  74. def test_new_should_select_default_activity
  75. @request.session[:user_id] = 3
  76. get :new, :params => {:project_id => 1}
  77. assert_response :success
  78. assert_select 'select[name=?]', 'time_entry[activity_id]' do
  79. assert_select 'option[selected=selected]', :text => 'Development'
  80. end
  81. end
  82. def test_new_should_only_show_active_time_entry_activities
  83. @request.session[:user_id] = 3
  84. get :new, :params => {:project_id => 1}
  85. assert_response :success
  86. assert_select 'option', :text => 'Inactive Activity', :count => 0
  87. end
  88. def test_new_should_show_user_select_if_user_has_permission
  89. Role.find_by_name('Manager').add_permission! :log_time_for_other_users
  90. @request.session[:user_id] = 2
  91. get :new, :params => {:project_id => 1}
  92. assert_response :success
  93. assert_select 'select[name=?]', 'time_entry[user_id]' do
  94. assert_select 'option', 3
  95. assert_select 'option[value=?]', '2', 2
  96. assert_select 'option[value=?]', '3', 1
  97. # locked members should not be available
  98. assert_select 'option[value=?]', '4', 0
  99. end
  100. end
  101. def test_new_user_select_should_include_current_user_if_is_logged
  102. @request.session[:user_id] = 1
  103. get :new, :params => {:project_id => 1}
  104. assert_response :success
  105. assert_select 'select[name=?]', 'time_entry[user_id]' do
  106. assert_select 'option[value=?]', '1', :text => '<< me >>'
  107. assert_select 'option[value=?]', '1', :text => 'Redmine Admin'
  108. end
  109. end
  110. def test_new_should_not_show_user_select_if_user_does_not_have_permission
  111. @request.session[:user_id] = 2
  112. get :new, :params => {:project_id => 1}
  113. assert_response :success
  114. assert_select 'select[name=?]', 'time_entry[user_id]', 0
  115. end
  116. def test_post_new_as_js_should_update_activity_options
  117. @request.session[:user_id] = 3
  118. post :new, :params => {:time_entry => {:project_id => 1}, :format => 'js'}
  119. assert_response :success
  120. assert_include '#time_entry_activity_id', response.body
  121. end
  122. def test_get_edit_existing_time
  123. @request.session[:user_id] = 2
  124. get :edit, :params => {:id => 2, :project_id => nil}
  125. assert_response :success
  126. assert_select 'form[action=?]', '/time_entries/2'
  127. end
  128. def test_get_edit_with_an_existing_time_entry_with_inactive_activity
  129. te = TimeEntry.find(1)
  130. te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
  131. te.save!(:validate => false)
  132. @request.session[:user_id] = 1
  133. get :edit, :params => {:project_id => 1, :id => 1}
  134. assert_response :success
  135. # Blank option since nothing is pre-selected
  136. assert_select 'option', :text => '--- Please select ---'
  137. end
  138. def test_get_edit_should_show_projects_select
  139. @request.session[:user_id] = 2
  140. get :edit, :params => {:id => 2, :project_id => nil}
  141. assert_response :success
  142. assert_select 'select[name=?]', 'time_entry[project_id]'
  143. end
  144. def test_post_create
  145. @request.session[:user_id] = 3
  146. assert_difference 'TimeEntry.count' do
  147. post :create, :params => {
  148. :project_id => 1,
  149. :time_entry => {:comments => 'Some work on TimelogControllerTest',
  150. # Not the default activity
  151. :activity_id => '11',
  152. :spent_on => '2008-03-14',
  153. :issue_id => '1',
  154. :hours => '7.3'
  155. }
  156. }
  157. assert_redirected_to '/projects/ecookbook/time_entries'
  158. end
  159. t = TimeEntry.order('id DESC').first
  160. assert_not_nil t
  161. assert_equal 'Some work on TimelogControllerTest', t.comments
  162. assert_equal 1, t.project_id
  163. assert_equal 1, t.issue_id
  164. assert_equal 11, t.activity_id
  165. assert_equal 7.3, t.hours
  166. assert_equal 3, t.user_id
  167. end
  168. def test_post_create_with_blank_issue
  169. @request.session[:user_id] = 3
  170. assert_difference 'TimeEntry.count' do
  171. post :create, :params => {
  172. :project_id => 1,
  173. :time_entry => {
  174. :comments => 'Some work on TimelogControllerTest',
  175. # Not the default activity
  176. :activity_id => '11',
  177. :issue_id => '',
  178. :spent_on => '2008-03-14',
  179. :hours => '7.3'
  180. }
  181. }
  182. assert_redirected_to '/projects/ecookbook/time_entries'
  183. end
  184. t = TimeEntry.order('id DESC').first
  185. assert_not_nil t
  186. assert_equal 'Some work on TimelogControllerTest', t.comments
  187. assert_equal 1, t.project_id
  188. assert_nil t.issue_id
  189. assert_equal 11, t.activity_id
  190. assert_equal 7.3, t.hours
  191. assert_equal 3, t.user_id
  192. end
  193. def test_create_on_project_with_time_tracking_disabled_should_fail
  194. Project.find(1).disable_module! :time_tracking
  195. @request.session[:user_id] = 2
  196. assert_no_difference 'TimeEntry.count' do
  197. post :create, :params => {
  198. :time_entry => {
  199. :project_id => '1', :issue_id => '',
  200. :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
  201. }
  202. }
  203. end
  204. end
  205. def test_create_on_project_without_permission_should_fail
  206. Role.find(1).remove_permission! :log_time
  207. @request.session[:user_id] = 2
  208. assert_no_difference 'TimeEntry.count' do
  209. post :create, :params => {
  210. :time_entry => {
  211. :project_id => '1', :issue_id => '',
  212. :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
  213. }
  214. }
  215. end
  216. end
  217. def test_create_on_issue_in_project_with_time_tracking_disabled_should_fail
  218. Project.find(1).disable_module! :time_tracking
  219. @request.session[:user_id] = 2
  220. assert_no_difference 'TimeEntry.count' do
  221. post :create, :params => {
  222. :time_entry => {
  223. :project_id => '', :issue_id => '1',
  224. :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
  225. }
  226. }
  227. assert_select_error /Issue is invalid/
  228. end
  229. end
  230. def test_create_on_issue_in_project_without_permission_should_fail
  231. Role.find(1).remove_permission! :log_time
  232. @request.session[:user_id] = 2
  233. assert_no_difference 'TimeEntry.count' do
  234. post :create, :params => {
  235. :time_entry => {
  236. :project_id => '', :issue_id => '1',
  237. :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
  238. }
  239. }
  240. assert_select_error /Issue is invalid/
  241. end
  242. end
  243. def test_create_on_issue_that_is_not_visible_should_not_disclose_subject
  244. issue = Issue.generate!(:subject => "issue_that_is_not_visible", :is_private => true)
  245. assert !issue.visible?(User.find(3))
  246. @request.session[:user_id] = 3
  247. assert_no_difference 'TimeEntry.count' do
  248. post :create, :params => {
  249. :time_entry => {
  250. :project_id => '', :issue_id => issue.id.to_s,
  251. :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
  252. }
  253. }
  254. end
  255. assert_select_error /Issue is invalid/
  256. assert_select "input[name=?][value=?]", "time_entry[issue_id]", issue.id.to_s
  257. assert_select "#time_entry_issue a", 0
  258. assert !response.body.include?('issue_that_is_not_visible')
  259. end
  260. def test_create_for_other_user
  261. Role.find_by_name('Manager').add_permission! :log_time_for_other_users
  262. @request.session[:user_id] = 2
  263. post :create, :params => {
  264. :project_id => 1,
  265. :time_entry => {:comments => 'Some work on TimelogControllerTest',
  266. # Not the default activity
  267. :activity_id => '11',
  268. :spent_on => '2008-03-14',
  269. :issue_id => '1',
  270. :hours => '7.3',
  271. :user_id => '3'
  272. }
  273. }
  274. assert_redirected_to '/projects/ecookbook/time_entries'
  275. t = TimeEntry.last
  276. assert_equal 3, t.user_id
  277. assert_equal 2, t.author_id
  278. end
  279. def test_create_for_other_user_should_deny_for_user_without_permission
  280. Role.find_by_name('Manager').remove_permission! :log_time_for_other_users
  281. @request.session[:user_id] = 2
  282. post :create, :params => {
  283. :project_id => 1,
  284. :time_entry => {:comments => 'Some work on TimelogControllerTest',
  285. # Not the default activity
  286. :activity_id => '11',
  287. :spent_on => '2008-03-14',
  288. :issue_id => '1',
  289. :hours => '7.3',
  290. :user_id => '3'
  291. }
  292. }
  293. assert_response 403
  294. assert_select 'p[id=?]', 'errorExplanation', :text => 'Your role is not allowed to log time for other users'
  295. end
  296. def test_create_and_continue_at_project_level
  297. @request.session[:user_id] = 2
  298. assert_difference 'TimeEntry.count' do
  299. post :create, :params => {
  300. :time_entry => {
  301. :project_id => '1',
  302. :activity_id => '11',
  303. :issue_id => '',
  304. :spent_on => '2008-03-14',
  305. :hours => '7.3'
  306. },
  307. :continue => '1'
  308. }
  309. assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D=1&time_entry%5Bspent_on%5D=2008-03-14'
  310. end
  311. end
  312. def test_create_and_continue_at_issue_level
  313. @request.session[:user_id] = 2
  314. assert_difference 'TimeEntry.count' do
  315. post :create, :params => {
  316. :time_entry => {
  317. :project_id => '',
  318. :activity_id => '11',
  319. :issue_id => '1',
  320. :spent_on => '2008-03-14',
  321. :hours => '7.3'
  322. },
  323. :continue => '1'
  324. }
  325. assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1&time_entry%5Bproject_id%5D=&time_entry%5Bspent_on%5D=2008-03-14'
  326. end
  327. end
  328. def test_create_and_continue_with_project_id
  329. @request.session[:user_id] = 2
  330. assert_difference 'TimeEntry.count' do
  331. post :create, :params => {
  332. :project_id => 1,
  333. :time_entry => {
  334. :activity_id => '11',
  335. :issue_id => '',
  336. :spent_on => '2008-03-14',
  337. :hours => '7.3'
  338. },
  339. :continue => '1'
  340. }
  341. assert_redirected_to '/projects/ecookbook/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D=&time_entry%5Bspent_on%5D=2008-03-14'
  342. end
  343. end
  344. def test_create_and_continue_with_issue_id
  345. @request.session[:user_id] = 2
  346. assert_difference 'TimeEntry.count' do
  347. post :create, :params => {
  348. :issue_id => 1,
  349. :time_entry => {
  350. :activity_id => '11',
  351. :issue_id => '1',
  352. :spent_on => '2008-03-14',
  353. :hours => '7.3'
  354. },
  355. :continue => '1'
  356. }
  357. assert_redirected_to '/issues/1/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1&time_entry%5Bproject_id%5D=&time_entry%5Bspent_on%5D=2008-03-14'
  358. end
  359. end
  360. def test_create_without_log_time_permission_should_be_denied
  361. @request.session[:user_id] = 2
  362. Role.find_by_name('Manager').remove_permission! :log_time
  363. post :create, :params => {
  364. :project_id => 1,
  365. :time_entry => {
  366. :activity_id => '11',
  367. :issue_id => '',
  368. :spent_on => '2008-03-14',
  369. :hours => '7.3'
  370. }
  371. }
  372. assert_response 403
  373. end
  374. def test_create_without_project_and_issue_should_fail
  375. @request.session[:user_id] = 2
  376. post :create, :params => {:time_entry => {:issue_id => ''}}
  377. assert_response :success
  378. assert_select_error /Project cannot be blank/
  379. end
  380. def test_create_with_failure
  381. @request.session[:user_id] = 2
  382. post :create, :params => {
  383. :project_id => 1,
  384. :time_entry => {
  385. :activity_id => '',
  386. :issue_id => '',
  387. :spent_on => '2008-03-14',
  388. :hours => '7.3'
  389. }
  390. }
  391. assert_response :success
  392. end
  393. def test_create_without_project
  394. @request.session[:user_id] = 2
  395. assert_difference 'TimeEntry.count' do
  396. post :create, :params => {
  397. :time_entry => {
  398. :project_id => '1',
  399. :activity_id => '11',
  400. :issue_id => '',
  401. :spent_on => '2008-03-14',
  402. :hours => '7.3'
  403. }
  404. }
  405. end
  406. assert_redirected_to '/projects/ecookbook/time_entries'
  407. time_entry = TimeEntry.order('id DESC').first
  408. assert_equal 1, time_entry.project_id
  409. end
  410. def test_create_without_project_should_fail_with_issue_not_inside_project
  411. @request.session[:user_id] = 2
  412. assert_no_difference 'TimeEntry.count' do
  413. post :create, :params => {
  414. :time_entry => {
  415. :project_id => '1',
  416. :activity_id => '11',
  417. :issue_id => '5',
  418. :spent_on => '2008-03-14',
  419. :hours => '7.3'
  420. }
  421. }
  422. end
  423. assert_response :success
  424. assert_select_error /Issue is invalid/
  425. end
  426. def test_create_without_project_should_deny_without_permission
  427. @request.session[:user_id] = 2
  428. Project.find(3).disable_module!(:time_tracking)
  429. assert_no_difference 'TimeEntry.count' do
  430. post :create, :params => {
  431. :time_entry => {
  432. :project_id => '3',
  433. :activity_id => '11',
  434. :issue_id => '',
  435. :spent_on => '2008-03-14',
  436. :hours => '7.3'
  437. }
  438. }
  439. end
  440. assert_response 403
  441. end
  442. def test_create_without_project_with_failure
  443. @request.session[:user_id] = 2
  444. assert_no_difference 'TimeEntry.count' do
  445. post :create, :params => {
  446. :time_entry => {
  447. :project_id => '1',
  448. :activity_id => '11',
  449. :issue_id => '',
  450. :spent_on => '2008-03-14',
  451. :hours => ''
  452. }
  453. }
  454. end
  455. assert_response :success
  456. assert_select 'select[name=?]', 'time_entry[project_id]' do
  457. assert_select 'option[value="1"][selected=selected]'
  458. end
  459. end
  460. def test_update
  461. entry = TimeEntry.find(1)
  462. assert_equal 1, entry.issue_id
  463. assert_equal 2, entry.user_id
  464. @request.session[:user_id] = 1
  465. put :update, :params => {
  466. :id => 1,
  467. :time_entry => {
  468. :issue_id => '2',
  469. :hours => '8'
  470. }
  471. }
  472. assert_redirected_to :action => 'index', :project_id => 'ecookbook'
  473. entry.reload
  474. assert_equal 8, entry.hours
  475. assert_equal 2, entry.issue_id
  476. assert_equal 2, entry.user_id
  477. end
  478. def test_update_should_allow_to_change_issue_to_another_project
  479. entry = TimeEntry.generate!(:issue_id => 1)
  480. @request.session[:user_id] = 1
  481. put :update, :params => {
  482. :id => entry.id,
  483. :time_entry => {
  484. :issue_id => '5'
  485. }
  486. }
  487. assert_response 302
  488. entry.reload
  489. assert_equal 5, entry.issue_id
  490. assert_equal 3, entry.project_id
  491. end
  492. def test_update_should_not_allow_to_change_issue_to_an_invalid_project
  493. entry = TimeEntry.generate!(:issue_id => 1)
  494. Project.find(3).disable_module!(:time_tracking)
  495. @request.session[:user_id] = 1
  496. put :update, :params => {
  497. :id => entry.id,
  498. :time_entry => {
  499. :issue_id => '5'
  500. }
  501. }
  502. assert_response :success
  503. assert_select_error /Issue is invalid/
  504. end
  505. def test_update_should_allow_to_change_project
  506. entry = TimeEntry.generate!(:project_id => 1)
  507. @request.session[:user_id] = 1
  508. put :update, :params => {
  509. :id => entry.id,
  510. :time_entry => {
  511. :project_id => '2'
  512. }
  513. }
  514. assert_response 302
  515. entry.reload
  516. assert_equal 2, entry.project_id
  517. end
  518. def test_update_should_fail_with_issue_from_another_project
  519. entry = TimeEntry.generate!(:project_id => 1, :issue_id => 1)
  520. @request.session[:user_id] = 1
  521. put :update, :params => {
  522. :id => entry.id,
  523. :time_entry => {
  524. :project_id => '2'
  525. }
  526. }
  527. assert_response :success
  528. assert_select_error /Issue is invalid/
  529. end
  530. def test_update_should_deny_changing_user_for_user_without_permission
  531. Role.find_by_name('Manager').remove_permission! :log_time_for_other_users
  532. @request.session[:user_id] = 2
  533. put :update, :params => {
  534. :id => 3,
  535. :time_entry => {
  536. :user_id => '3'
  537. }
  538. }
  539. assert_response 403
  540. assert_select 'p[id=?]', 'errorExplanation', :text => 'Your role is not allowed to log time for other users'
  541. end
  542. def test_get_bulk_edit
  543. @request.session[:user_id] = 2
  544. get :bulk_edit, :params => {:ids => [1, 2]}
  545. assert_response :success
  546. assert_select 'ul#bulk-selection' do
  547. assert_select 'li', 2
  548. assert_select 'li a', :text => '03/23/2007 - eCookbook: 4.25 hours'
  549. end
  550. assert_select 'form#bulk_edit_form[action=?]', '/time_entries/bulk_update' do
  551. assert_select 'select[name=?]', 'time_entry[project_id]'
  552. # Clear issue checkbox
  553. assert_select 'input[name=?][value=?]', 'time_entry[issue_id]', 'none'
  554. # System wide custom field
  555. assert_select 'select[name=?]', 'time_entry[custom_field_values][10]'
  556. # Activities
  557. assert_select 'select[name=?]', 'time_entry[activity_id]' do
  558. assert_select 'option[value=""]', :text => '(No change)'
  559. assert_select 'option[value="9"]', :text => 'Design'
  560. end
  561. end
  562. end
  563. def test_get_bulk_edit_on_different_projects
  564. @request.session[:user_id] = 2
  565. get :bulk_edit, :params => {:ids => [1, 2, 6]}
  566. assert_response :success
  567. end
  568. def test_get_bulk_edit_on_different_projects_should_propose_only_common_activites
  569. project = Project.find(3)
  570. TimeEntryActivity.create!(:name => 'QA', :project => project, :parent => TimeEntryActivity.find_by_name('QA'), :active => false)
  571. @request.session[:user_id] = 1
  572. get :bulk_edit, :params => {:ids => [1, 2, 4]}
  573. assert_response :success
  574. assert_select 'select[id=?]', 'time_entry_activity_id' do
  575. assert_select 'option', 3
  576. assert_select 'option[value=?]', '11', 0, :text => 'QA'
  577. end
  578. end
  579. def test_get_bulk_edit_on_same_project_should_propose_project_activities
  580. project = Project.find(1)
  581. override_activity = TimeEntryActivity.create!({:name => "QA override", :parent => TimeEntryActivity.find_by_name("QA"), :project => project})
  582. @request.session[:user_id] = 1
  583. get :bulk_edit, :params => {:ids => [1, 2]}
  584. assert_response :success
  585. assert_select 'select[id=?]', 'time_entry_activity_id' do
  586. assert_select 'option', 4
  587. assert_select 'option[value=?]', override_activity.id.to_s, :text => 'QA override'
  588. end
  589. end
  590. def test_bulk_edit_with_edit_own_time_entries_permission
  591. @request.session[:user_id] = 2
  592. Role.find_by_name('Manager').remove_permission! :edit_time_entries
  593. Role.find_by_name('Manager').add_permission! :edit_own_time_entries
  594. ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id}
  595. get :bulk_edit, :params => {:ids => ids}
  596. assert_response :success
  597. end
  598. def test_bulk_update
  599. @request.session[:user_id] = 2
  600. # update time entry activity
  601. post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :activity_id => 9}}
  602. assert_response 302
  603. # check that the issues were updated
  604. assert_equal [9, 9], TimeEntry.where(:id => [1, 2]).collect {|i| i.activity_id}
  605. end
  606. def test_bulk_update_with_failure
  607. @request.session[:user_id] = 2
  608. post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :hours => 'A'}}
  609. assert_response :success
  610. assert_select_error /Failed to save 2 time entrie/
  611. end
  612. def test_bulk_update_on_different_projects
  613. @request.session[:user_id] = 2
  614. # makes user a manager on the other project
  615. Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
  616. # update time entry activity
  617. post :bulk_update, :params => {:ids => [1, 2, 4], :time_entry => { :activity_id => 9 }}
  618. assert_response 302
  619. # check that the issues were updated
  620. assert_equal [9, 9, 9], TimeEntry.where(:id => [1, 2, 4]).collect {|i| i.activity_id}
  621. end
  622. def test_bulk_update_on_different_projects_without_rights
  623. @request.session[:user_id] = 3
  624. user = User.find(3)
  625. action = { :controller => "timelog", :action => "bulk_update" }
  626. assert user.allowed_to?(action, TimeEntry.find(1).project)
  627. assert ! user.allowed_to?(action, TimeEntry.find(5).project)
  628. post :bulk_update, :params => {:ids => [1, 5], :time_entry => { :activity_id => 9 }}
  629. assert_response 403
  630. end
  631. def test_bulk_update_with_edit_own_time_entries_permission
  632. @request.session[:user_id] = 2
  633. Role.find_by_name('Manager').remove_permission! :edit_time_entries
  634. Role.find_by_name('Manager').add_permission! :edit_own_time_entries
  635. ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id}
  636. post :bulk_update, :params => {:ids => ids, :time_entry => { :activity_id => 9 }}
  637. assert_response 302
  638. end
  639. def test_bulk_update_with_edit_own_time_entries_permissions_should_be_denied_for_time_entries_of_other_user
  640. @request.session[:user_id] = 2
  641. Role.find_by_name('Manager').remove_permission! :edit_time_entries
  642. Role.find_by_name('Manager').add_permission! :edit_own_time_entries
  643. post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :activity_id => 9 }}
  644. assert_response 403
  645. end
  646. def test_bulk_update_custom_field
  647. @request.session[:user_id] = 2
  648. post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }}
  649. assert_response 302
  650. assert_equal ["0", "0"], TimeEntry.where(:id => [1, 2]).collect {|i| i.custom_value_for(10).value}
  651. end
  652. def test_bulk_update_clear_custom_field
  653. field = TimeEntryCustomField.generate!(:field_format => 'string')
  654. @request.session[:user_id] = 2
  655. post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :custom_field_values => {field.id.to_s => '__none__'} }}
  656. assert_response 302
  657. assert_equal ["", ""], TimeEntry.where(:id => [1, 2]).collect {|i| i.custom_value_for(field).value}
  658. end
  659. def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
  660. @request.session[:user_id] = 2
  661. post :bulk_update, :params => {:ids => [1,2], :back_url => '/time_entries'}
  662. assert_response :redirect
  663. assert_redirected_to '/time_entries'
  664. end
  665. def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
  666. @request.session[:user_id] = 2
  667. post :bulk_update, :params => {:ids => [1,2], :back_url => 'http://google.com'}
  668. assert_response :redirect
  669. assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
  670. end
  671. def test_post_bulk_update_without_edit_permission_should_be_denied
  672. @request.session[:user_id] = 2
  673. Role.find_by_name('Manager').remove_permission! :edit_time_entries
  674. post :bulk_update, :params => {:ids => [1,2]}
  675. assert_response 403
  676. end
  677. def test_destroy
  678. @request.session[:user_id] = 2
  679. delete :destroy, :params => {:id => 1}
  680. assert_redirected_to :action => 'index', :project_id => 'ecookbook'
  681. assert_equal I18n.t(:notice_successful_delete), flash[:notice]
  682. assert_nil TimeEntry.find_by_id(1)
  683. end
  684. def test_destroy_should_fail
  685. # simulate that this fails (e.g. due to a plugin), see #5700
  686. TimeEntry.any_instance.expects(:destroy).returns(false)
  687. @request.session[:user_id] = 2
  688. delete :destroy, :params => {:id => 1}
  689. assert_redirected_to :action => 'index', :project_id => 'ecookbook'
  690. assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
  691. assert_not_nil TimeEntry.find_by_id(1)
  692. end
  693. def test_destroy_should_redirect_to_referer
  694. referer = 'http://test.host/time_entries?utf8=✓&set_filter=1&&f%5B%5D=user_id&op%5Buser_id%5D=%3D&v%5Buser_id%5D%5B%5D=me'
  695. @request.env["HTTP_REFERER"] = referer
  696. @request.session[:user_id] = 2
  697. delete :destroy, :params => {:id => 1}
  698. assert_redirected_to referer
  699. end
  700. def test_index_all_projects
  701. get :index
  702. assert_response :success
  703. assert_select '.total-for-hours', :text => 'Hours: 162.90'
  704. assert_select 'form#query_form[action=?]', '/time_entries'
  705. assert_equal ['Project', 'Date', 'User', 'Activity', 'Issue', 'Comment', 'Hours'], columns_in_list
  706. assert_select '.query-totals>span', 1
  707. end
  708. def test_index_with_default_query_setting
  709. with_settings :time_entry_list_defaults => {'column_names' => %w(spent_on issue user hours), 'totalable_names' => []} do
  710. get :index
  711. assert_response :success
  712. end
  713. assert_select 'table.time-entries thead' do
  714. assert_select 'th.project'
  715. assert_select 'th.spent_on'
  716. assert_select 'th.issue'
  717. assert_select 'th.user'
  718. assert_select 'th.hours'
  719. end
  720. assert_select 'table.time-entries tbody' do
  721. assert_select 'td.project'
  722. assert_select 'td.spent_on'
  723. assert_select 'td.issue'
  724. assert_select 'td.user'
  725. assert_select 'td.hours'
  726. end
  727. assert_equal ['Project', 'Date', 'Issue', 'User', 'Hours'], columns_in_list
  728. end
  729. def test_index_with_default_query_setting_using_custom_field
  730. field = TimeEntryCustomField.create!(:name => 'Foo', :field_format => 'int')
  731. with_settings :time_entry_list_defaults => {
  732. 'column_names' => ["spent_on", "user", "hours", "cf_#{field.id}"],
  733. 'totalable_names' => ["hours", "cf_#{field.id}"]
  734. } do
  735. get :index
  736. assert_response :success
  737. end
  738. assert_equal ['Project', 'Date', 'User', 'Hours', 'Foo'], columns_in_list
  739. assert_select '.total-for-hours'
  740. assert_select ".total-for-cf-#{field.id}"
  741. assert_select '.query-totals>span', 2
  742. end
  743. def test_index_all_projects_should_show_log_time_link
  744. @request.session[:user_id] = 2
  745. get :index
  746. assert_response :success
  747. assert_select 'a[href=?]', '/time_entries/new', :text => /Log time/
  748. end
  749. def test_index_my_spent_time
  750. @request.session[:user_id] = 2
  751. get :index, :params => {:user_id => 'me', :c => ['user']}
  752. assert_response :success
  753. users = css_select('table.time-entries tbody td.user').map(&:text).uniq
  754. assert_equal ["John Smith"], users
  755. end
  756. def test_index_at_project_level
  757. @request.session[:user_id] = 2
  758. get :index, :params => {:project_id => 'ecookbook', :c => ['project']}
  759. assert_response :success
  760. assert_select 'tr.time-entry', 4
  761. # project and subproject
  762. projects = css_select('table.time-entries tbody td.project').map(&:text).uniq.sort
  763. assert_equal ["eCookbook", "eCookbook Subproject 1"], projects
  764. assert_select '.total-for-hours', :text => 'Hours: 162.90'
  765. assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
  766. # 'Log time' shoudl link to log time on the filtered issue
  767. assert_select 'a[href=?]', "/projects/ecookbook/time_entries/new"
  768. end
  769. def test_index_with_display_subprojects_issues_to_false_should_not_include_subproject_entries
  770. entry = TimeEntry.generate!(:project => Project.find(3))
  771. with_settings :display_subprojects_issues => '0' do
  772. get :index, :params => {:project_id => 'ecookbook', :c => ['project']}
  773. assert_response :success
  774. projects = css_select('table.time-entries tbody td.project').map(&:text).uniq.sort
  775. assert_equal ["eCookbook"], projects
  776. end
  777. end
  778. def test_index_with_display_subprojects_issues_to_false_and_subproject_filter_should_include_subproject_entries
  779. entry = TimeEntry.generate!(:project => Project.find(3))
  780. with_settings :display_subprojects_issues => '0' do
  781. get :index, :params => {:project_id => 'ecookbook', :c => ['project'], :subproject_id => 3}
  782. assert_response :success
  783. projects = css_select('table.time-entries tbody td.project').map(&:text).uniq.sort
  784. assert_equal ["eCookbook", "eCookbook Subproject 1"], projects
  785. end
  786. end
  787. def test_index_at_project_level_with_issue_id_short_filter
  788. issue = Issue.generate!(:project_id => 1)
  789. TimeEntry.generate!(:issue => issue, :hours => 4)
  790. TimeEntry.generate!(:issue => issue, :hours => 3)
  791. @request.session[:user_id] = 2
  792. get :index, :params => {:project_id => 'ecookbook', :issue_id => issue.id.to_s, :set_filter => 1}
  793. assert_select '.total-for-hours', :text => 'Hours: 7.00'
  794. # 'Log time' shoudl link to log time on the filtered issue
  795. assert_select 'a[href=?]', "/issues/#{issue.id}/time_entries/new"
  796. end
  797. def test_index_at_project_level_with_issue_fixed_version_id_short_filter
  798. version = Version.generate!(:project_id => 1)
  799. issue = Issue.generate!(:project_id => 1, :fixed_version => version)
  800. TimeEntry.generate!(:issue => issue, :hours => 2)
  801. TimeEntry.generate!(:issue => issue, :hours => 3)
  802. @request.session[:user_id] = 2
  803. get :index, :params => {:project_id => 'ecookbook', :"issue.fixed_version_id" => version.id.to_s, :set_filter => 1}
  804. assert_select '.total-for-hours', :text => 'Hours: 5.00'
  805. end
  806. def test_index_at_project_level_with_multiple_issue_fixed_version_ids
  807. version = Version.generate!(:project_id => 1)
  808. version2 = Version.generate!(:project_id => 1)
  809. issue = Issue.generate!(:project_id => 1, :fixed_version => version)
  810. issue2 = Issue.generate!(:project_id => 1, :fixed_version => version2)
  811. TimeEntry.generate!(:issue => issue, :hours => 2)
  812. TimeEntry.generate!(:issue => issue2, :hours => 3)
  813. @request.session[:user_id] = 2
  814. get :index, :params => {
  815. :project_id => 'ecookbook',
  816. :f => ['issue.fixed_version_id'],
  817. :op => {'issue.fixed_version_id' => '='},
  818. :v => {'issue.fixed_version_id' => [version.id.to_s,version2.id.to_s]}
  819. }
  820. assert_response :success
  821. assert_select 'tr.time-entry', 2
  822. assert_select '.total-for-hours', :text => 'Hours: 5.00'
  823. end
  824. def test_index_at_project_level_with_date_range
  825. get :index, :params => {
  826. :project_id => 'ecookbook',
  827. :f => ['spent_on'],
  828. :op => {'spent_on' => '><'},
  829. :v => {'spent_on' => ['2007-03-20', '2007-04-30']}
  830. }
  831. assert_response :success
  832. assert_select 'tr.time-entry', 3
  833. assert_select '.total-for-hours', :text => 'Hours: 12.90'
  834. assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
  835. end
  836. def test_index_at_project_level_with_date_range_using_from_and_to_params
  837. get :index, :params => {
  838. :project_id => 'ecookbook',
  839. :from => '2007-03-20',
  840. :to => '2007-04-30'
  841. }
  842. assert_response :success
  843. assert_select 'tr.time-entry', 3
  844. assert_select '.total-for-hours', :text => 'Hours: 12.90'
  845. assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
  846. end
  847. def test_index_at_project_level_with_period
  848. get :index, :params => {
  849. :project_id => 'ecookbook',
  850. :f => ['spent_on'],
  851. :op => {'spent_on' => '>t-'},
  852. :v => {'spent_on' => ['7']}
  853. }
  854. assert_response :success
  855. assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
  856. end
  857. def test_index_should_sort_by_spent_on_and_created_on
  858. t1 = TimeEntry.create!(:author => User.find(1), :user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-16', :created_on => '2012-06-16 20:00:00', :activity_id => 10)
  859. t2 = TimeEntry.create!(:author => User.find(1), :user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-16', :created_on => '2012-06-16 20:05:00', :activity_id => 10)
  860. t3 = TimeEntry.create!(:author => User.find(1), :user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-15', :created_on => '2012-06-16 20:10:00', :activity_id => 10)
  861. get :index, :params => {
  862. :project_id => 1,
  863. :f => ['spent_on'],
  864. :op => {'spent_on' => '><'},
  865. :v => {'spent_on' => ['2012-06-15', '2012-06-16']}
  866. }
  867. assert_response :success
  868. assert_equal [t2, t1, t3].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
  869. get :index, :params => {
  870. :project_id => 1,
  871. :f => ['spent_on'],
  872. :op => {'spent_on' => '><'},
  873. :v => {'spent_on' => ['2012-06-15', '2012-06-16']},
  874. :sort => 'spent_on'
  875. }
  876. assert_response :success
  877. assert_equal [t3, t1, t2].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
  878. end
  879. def test_index_with_activity_filter
  880. activity = TimeEntryActivity.create!(:name => 'Activity')
  881. entry = TimeEntry.generate!(:issue_id => 1, :hours => 4.5, :activity => activity)
  882. get :index, :params => {
  883. :f => ['activity_id'],
  884. :op => {'activity_id' => '='},
  885. :v => {'activity_id' => [activity.id.to_s]}
  886. }
  887. assert_response :success
  888. assert_select "tr#time-entry-#{entry.id}"
  889. assert_select "table.time-entries tbody tr", 1
  890. end
  891. def test_index_with_issue_status_filter
  892. Issue.where(:status_id => 4).update_all(:status_id => 2)
  893. issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 4)
  894. entry = TimeEntry.generate!(:issue => issue, :hours => 4.5)
  895. get :index, :params => {
  896. :f => ['issue.status_id'],
  897. :op => {'issue.status_id' => '='},
  898. :v => {'issue.status_id' => ['4']}
  899. }
  900. assert_response :success
  901. assert_equal [entry].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
  902. end
  903. def test_index_with_project_status_filter
  904. project = Project.find(3)
  905. project.close
  906. project.save
  907. get :index, :params => {
  908. :set_filter => 1,
  909. :f => ['project.status'],
  910. :op => {'project.status' => '='},
  911. :v => {'project.status' => ['1']}
  912. }
  913. assert_response :success
  914. time_entries = css_select('input[name="ids[]"]').map {|e| e.attr('value')}
  915. assert_include '1', time_entries
  916. assert_not_include '4', time_entries
  917. end
  918. def test_index_with_issue_status_column
  919. issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 4)
  920. entry = TimeEntry.generate!(:issue => issue)
  921. get :index, :params => {
  922. :c => %w(project spent_on issue comments hours issue.status)
  923. }
  924. assert_response :success
  925. assert_select 'th.issue-status'
  926. assert_select 'td.issue-status', :text => issue.status.name
  927. end
  928. def test_index_with_issue_status_sort
  929. TimeEntry.delete_all
  930. TimeEntry.generate!(:issue => Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 1))
  931. TimeEntry.generate!(:issue => Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 5))
  932. TimeEntry.generate!(:issue => Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 3))
  933. TimeEntry.generate!(:project_id => 1)
  934. get :index, :params => {
  935. :c => ["hours", 'issue.status'],
  936. :sort => 'issue.status'
  937. }
  938. assert_response :success
  939. # Make sure that values are properly sorted
  940. values = css_select("td.issue-status").map(&:text).reject(&:blank?)
  941. assert_equal IssueStatus.where(:id => [1, 5, 3]).sorted.pluck(:name), values
  942. end
  943. def test_index_with_issue_tracker_filter
  944. Issue.where(:tracker_id => 2).update_all(:tracker_id => 1)
  945. issue = Issue.generate!(:project_id => 1, :tracker_id => 2)
  946. entry = TimeEntry.generate!(:issue => issue, :hours => 4.5)
  947. get :index, :params => {
  948. :f => ['issue.tracker_id'],
  949. :op => {'issue.tracker_id' => '='},
  950. :v => {'issue.tracker_id' => ['2']}
  951. }
  952. assert_response :success
  953. assert_equal [entry].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
  954. end
  955. def test_index_with_issue_tracker_column
  956. issue = Issue.generate!(:project_id => 1, :tracker_id => 2)
  957. entry = TimeEntry.generate!(:issue => issue)
  958. get :index, :params => {
  959. :c => %w(project spent_on issue comments hours issue.tracker)
  960. }
  961. assert_response :success
  962. assert_select 'td.issue-tracker', :text => issue.tracker.name
  963. end
  964. def test_index_with_issue_tracker_sort
  965. TimeEntry.delete_all
  966. TimeEntry.generate!(:issue => Issue.generate!(:tracker_id => 1))
  967. TimeEntry.generate!(:issue => Issue.generate!(:tracker_id => 3))
  968. TimeEntry.generate!(:issue => Issue.generate!(:tracker_id => 2))
  969. TimeEntry.generate!(:project_id => 1)
  970. get :index, :params => {
  971. :c => ["hours", 'issue.tracker'],
  972. :sort => 'issue.tracker'
  973. }
  974. assert_response :success
  975. # Make sure that values are properly sorted
  976. values = css_select("td.issue-tracker").map(&:text).reject(&:blank?)
  977. assert_equal Tracker.where(:id => [1, 2, 3]).sorted.pluck(:name), values
  978. end
  979. def test_index_with_issue_category_filter
  980. get :index, :params => {
  981. :project_id => 'ecookbook',
  982. :f => ['issue.category_id'],
  983. :op => {'issue.category_id' => '='},
  984. :v => {'issue.category_id' => ['1']}
  985. }
  986. assert_response :success
  987. assert_equal ['1', '2'], css_select('input[name="ids[]"]').map {|e| e.attr('value')}
  988. end
  989. def test_index_with_issue_category_column
  990. get :index, :params => {
  991. :project_id => 'ecookbook',
  992. :c => %w(project spent_on issue comments hours issue.category)
  993. }
  994. assert_response :success
  995. assert_select 'td.issue-category', :text => 'Printing'
  996. end
  997. def test_index_with_author_filter
  998. get :index, :params => {
  999. :project_id => 'ecookbook',
  1000. :f => ['author_id'],
  1001. :op => {'author_id' => '='},
  1002. :v => {'author_id' => ['2']}
  1003. }
  1004. assert_response :success
  1005. assert_equal ['1'], css_select('input[name="ids[]"]').map {|e| e.attr('value')}
  1006. end
  1007. def test_index_with_author_column
  1008. get :index, :params => {
  1009. :project_id => 'ecookbook',
  1010. :c => %w(project spent_on issue comments hours author)
  1011. }
  1012. assert_response :success
  1013. assert_select 'td.author', :text => 'Redmine Admin'
  1014. end
  1015. def test_index_with_issue_category_sort
  1016. issue = Issue.find(3)
  1017. issue.category_id = 2
  1018. issue.save!
  1019. get :index, :params => {
  1020. :c => ["hours", 'issue.category'],
  1021. :sort => 'issue.category'
  1022. }
  1023. assert_response :success
  1024. # Make sure that values are properly sorted
  1025. values = css_select("td.issue-category").map(&:text).reject(&:blank?)
  1026. assert_equal ['Printing', 'Printing', 'Recipes'], values
  1027. end
  1028. def test_index_with_filter_on_issue_custom_field
  1029. issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
  1030. entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
  1031. get :index, :params => {
  1032. :f => ['issue.cf_2'],
  1033. :op => {'issue.cf_2' => '='},
  1034. :v => {'issue.cf_2' => ['filter_on_issue_custom_field']}
  1035. }
  1036. assert_response :success
  1037. assert_equal [entry].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
  1038. end
  1039. def test_index_with_issue_custom_field_column
  1040. issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
  1041. entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
  1042. get :index, :params => {
  1043. :c => %w(project spent_on issue comments hours issue.cf_2)
  1044. }
  1045. assert_response :success
  1046. assert_select 'td.issue_cf_2', :text => 'filter_on_issue_custom_field'
  1047. end
  1048. def test_index_with_time_entry_custom_field_column
  1049. field = TimeEntryCustomField.generate!(:field_format => 'string')
  1050. entry = TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value'})
  1051. field_name = "cf_#{field.id}"
  1052. get :index, :params => {
  1053. :c => ["hours", field_name]
  1054. }
  1055. assert_response :success
  1056. assert_select "td.#{field_name}", :text => 'CF Value'
  1057. end
  1058. def test_index_with_time_entry_custom_field_sorting
  1059. field = TimeEntryCustomField.generate!(:field_format => 'string', :name => 'String Field')
  1060. TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 1'})
  1061. TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 3'})
  1062. TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 2'})
  1063. field_name = "cf_#{field.id}"
  1064. get :index, :params => {
  1065. :c => ["hours", field_name],
  1066. :sort => field_name
  1067. }
  1068. assert_response :success
  1069. assert_select "th.cf_#{field.id} a.sort", :text => 'String Field'
  1070. # Make sure that values are properly sorted
  1071. values = css_select("td.#{field_name}").map(&:text).reject(&:blank?)
  1072. assert_equal values.sort, values
  1073. assert_equal 3, values.size
  1074. end
  1075. def test_index_with_invalid_date_filter_should_not_validate
  1076. @request.session[:user_id] = 2
  1077. get :index, :params => {:set_filter => '1', :f => ['spent_on'], :op => {'spent_on' => '='}, :v => {'spent_on' => ['2016-09-010']}}
  1078. assert_select_error 'Date is invalid'
  1079. assert_select 'table.time-entries', 0
  1080. end
  1081. def test_index_with_query
  1082. query = TimeEntryQuery.new(:project_id => 1, :name => 'Time Entry Query', :visibility => 2)
  1083. query.save!
  1084. @request.session[:user_id] = 2
  1085. get :index, :params => {:project_id => 'ecookbook', :query_id => query.id}
  1086. assert_response :success
  1087. assert_select 'h2', :text => query.name
  1088. assert_select '#sidebar a.selected', :text => query.name
  1089. end
  1090. def test_index_atom_feed
  1091. get :index, :params => {:project_id => 1, :format => 'atom'}
  1092. assert_response :success
  1093. assert_equal 'application/atom+xml', @response.content_type
  1094. assert_select 'entry > title', :text => /7\.65 hours/
  1095. end
  1096. def test_index_at_project_level_should_include_csv_export_dialog
  1097. get :index, :params => {
  1098. :project_id => 'ecookbook',
  1099. :f => ['spent_on'],
  1100. :op => {'spent_on' => '>='},
  1101. :v => {'spent_on' => ['2007-04-01']},
  1102. :c => ['spent_on', 'user']
  1103. }
  1104. assert_response :success
  1105. assert_select '#csv-export-options' do
  1106. assert_select 'form[action=?][method=get]', '/projects/ecookbook/time_entries.csv' do
  1107. # filter
  1108. assert_select 'input[name=?][value=?]', 'f[]', 'spent_on'
  1109. assert_select 'input[name=?][value=?]', 'op[spent_on]', '>='
  1110. assert_select 'input[name=?][value=?]', 'v[spent_on][]', '2007-04-01'
  1111. # columns
  1112. assert_select 'input[name=?][type=hidden][value=?]', 'c[]', 'spent_on'
  1113. assert_select 'input[name=?][type=hidden][value=?]', 'c[]', 'user'
  1114. assert_select 'input[name=?][type=hidden]', 'c[]', 2
  1115. assert_select 'input[name=?][value=?]', 'c[]', 'all_inline'
  1116. end
  1117. end
  1118. end
  1119. def test_index_cross_project_should_include_csv_export_dialog
  1120. get :index
  1121. assert_response :success
  1122. assert_select '#csv-export-options' do
  1123. assert_select 'form[action=?][method=get]', '/time_entries.csv'
  1124. end
  1125. end
  1126. def test_index_csv_all_projects
  1127. with_settings :date_format => '%m/%d/%Y' do
  1128. get :index, :params => {:format => 'csv'}
  1129. assert_response :success
  1130. assert_equal 'text/csv; header=present', response.content_type
  1131. end
  1132. end
  1133. def test_index_csv
  1134. with_settings :date_format => '%m/%d/%Y' do
  1135. get :index, :params => {:project_id => 1, :format => 'csv'}
  1136. assert_response :success
  1137. assert_equal 'text/csv; header=present', response.content_type
  1138. end
  1139. end
  1140. def test_index_csv_should_fill_issue_column_with_tracker_id_and_subject
  1141. issue = Issue.find(1)
  1142. entry = TimeEntry.generate!(:issue => issue, :comments => "Issue column content test")
  1143. get :index, :params => {:format => 'csv'}
  1144. line = response.body.split("\n").detect {|l| l.include?(entry.comments)}
  1145. assert_not_nil line
  1146. assert_include "#{issue.tracker} #1: #{issue.subject}", line
  1147. end
  1148. end