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.

queries_controller_test.rb 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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 QueriesControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :enabled_modules,
  21. :users, :email_addresses,
  22. :members, :member_roles, :roles,
  23. :trackers, :issue_statuses, :issue_categories, :enumerations, :versions,
  24. :issues, :custom_fields, :custom_values,
  25. :queries
  26. def setup
  27. User.current = nil
  28. end
  29. def test_index
  30. get :index
  31. # HTML response not implemented
  32. assert_response 406
  33. end
  34. def test_new_project_query
  35. @request.session[:user_id] = 2
  36. get(:new, :params => {:project_id => 1})
  37. assert_response :success
  38. assert_select 'input[name=?][value="0"][checked=checked]', 'query[visibility]'
  39. assert_select 'input[name=query_is_for_all][type=checkbox]:not([checked]):not([disabled])'
  40. assert_select 'select[name=?]', 'c[]' do
  41. assert_select 'option[value=tracker]'
  42. assert_select 'option[value=subject]'
  43. end
  44. end
  45. def test_new_global_query
  46. @request.session[:user_id] = 2
  47. get :new
  48. assert_response :success
  49. assert_select 'input[name=?]', 'query[visibility]', 0
  50. assert_select 'input[name=query_is_for_all][type=checkbox][checked]:not([disabled])'
  51. end
  52. def test_new_on_invalid_project
  53. @request.session[:user_id] = 2
  54. get(:new, :params => {:project_id => 'invalid'})
  55. assert_response 404
  56. end
  57. def test_new_should_not_render_show_inline_columns_option_for_query_without_available_inline_columns
  58. @request.session[:user_id] = 1
  59. get(:new, :params => {:type => 'ProjectQuery'})
  60. assert_response :success
  61. assert_select 'p[class=?]', 'block_columns', 0
  62. end
  63. def test_new_should_not_render_show_totals_option_for_query_without_totable_columns
  64. @request.session[:user_id] = 1
  65. get(:new, :params => {:type => 'ProjectQuery'})
  66. assert_response :success
  67. assert_select 'p[class=?]', 'totables_columns', 0
  68. end
  69. def test_new_time_entry_query
  70. @request.session[:user_id] = 2
  71. get(:new, :params => {:project_id => 1, :type => 'TimeEntryQuery'})
  72. assert_response :success
  73. assert_select 'input[name=type][value=?]', 'TimeEntryQuery'
  74. assert_select 'p[class=?]', 'totable_columns', 1
  75. assert_select 'p[class=?]', 'block_columns', 0
  76. end
  77. def test_new_project_query_for_projects
  78. @request.session[:user_id] = 1
  79. get(:new, :params => {:type => 'ProjectQuery'})
  80. assert_response :success
  81. assert_select 'input[name=type][value=?]', 'ProjectQuery'
  82. end
  83. def test_new_project_query_should_not_render_roles_visibility_options
  84. @request.session[:user_id] = 1
  85. get(:new, :params => {:type => 'ProjectQuery'})
  86. assert_response :success
  87. assert_select 'input[id=?]', 'query_visibility_0', 1
  88. assert_select 'input[id=?]', 'query_visibility_2', 1
  89. assert_select 'input[id=?]', 'query_visibility_1', 0
  90. end
  91. def test_new_project_query_should_not_render_for_all_projects_option
  92. @request.session[:user_id] = 1
  93. get(:new, :params => {:type => 'ProjectQuery'})
  94. assert_response :success
  95. assert_select 'input[name=?]', 'for_all_projects', 0
  96. end
  97. def test_new_time_entry_query_should_select_spent_time_from_main_menu
  98. @request.session[:user_id] = 2
  99. get(
  100. :new,
  101. :params => {
  102. :project_id => 1,
  103. :type => 'TimeEntryQuery'
  104. }
  105. )
  106. assert_response :success
  107. assert_select '#main-menu a.time-entries.selected'
  108. end
  109. def test_new_time_entry_query_with_issue_tracking_module_disabled_should_be_allowed
  110. Project.find(1).disable_module! :issue_tracking
  111. @request.session[:user_id] = 2
  112. get(
  113. :new,
  114. :params => {
  115. :project_id => 1,
  116. :type => 'TimeEntryQuery'
  117. }
  118. )
  119. assert_response :success
  120. end
  121. def test_new_with_gantt_params
  122. @request.session[:user_id] = 2
  123. get :new, :params => {:gantt => 1}
  124. assert_response :success
  125. assert_select 'input[type="hidden"]#gantt', 1
  126. assert_select 'fieldset#options'
  127. assert_select 'fieldset#filters'
  128. assert_select 'fieldset legend', {:text => 'Sort', :count => 0}
  129. assert_select 'fieldset#columns'
  130. end
  131. def test_new_with_calendar_params
  132. @request.session[:user_id] = 2
  133. get :new, :params => {:calendar => 1}
  134. assert_response :success
  135. assert_select 'input[type="hidden"]#calendar', 1
  136. assert_select 'fieldset#options', :count => 0
  137. assert_select 'fieldset#filters'
  138. assert_select 'fieldset legend', {:text => 'Sort', :count => 0}
  139. assert_select 'fieldset#columns', :count => 0
  140. end
  141. def test_new_without_gantt_and_calendar_params
  142. @request.session[:user_id] = 2
  143. get :new
  144. assert_response :success
  145. assert_select 'fieldset#options'
  146. assert_select 'fieldset#filters'
  147. assert_select 'fieldset legend', {:text => 'Sort'}
  148. assert_select 'fieldset#columns'
  149. end
  150. def test_create_project_public_query
  151. @request.session[:user_id] = 2
  152. post(
  153. :create,
  154. :params => {
  155. :project_id => 'ecookbook',
  156. :default_columns => '1',
  157. :f => ["status_id", "assigned_to_id"],
  158. :op => {
  159. "assigned_to_id" => "=", "status_id" => "o"
  160. },
  161. :v => {
  162. "assigned_to_id" => ["1"], "status_id" => ["1"]
  163. },
  164. :query => {
  165. "name" => "test_new_project_public_query", "visibility" => "2"
  166. }
  167. }
  168. )
  169. q = Query.find_by_name('test_new_project_public_query')
  170. assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :query_id => q
  171. assert q.is_public?
  172. assert q.has_default_columns?
  173. assert q.valid?
  174. end
  175. def test_create_project_private_query
  176. @request.session[:user_id] = 3
  177. post(
  178. :create,
  179. :params => {
  180. :project_id => 'ecookbook',
  181. :default_columns => '1',
  182. :fields => ["status_id", "assigned_to_id"],
  183. :operators => {
  184. "assigned_to_id" => "=", "status_id" => "o"
  185. },
  186. :values => {
  187. "assigned_to_id" => ["1"], "status_id" => ["1"]
  188. },
  189. :query => {
  190. "name" => "test_new_project_private_query", "visibility" => "0"
  191. }
  192. }
  193. )
  194. q = Query.find_by_name('test_new_project_private_query')
  195. assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :query_id => q
  196. assert !q.is_public?
  197. assert q.has_default_columns?
  198. assert q.valid?
  199. end
  200. def test_create_project_roles_query
  201. @request.session[:user_id] = 2
  202. post(
  203. :create,
  204. :params => {
  205. :project_id => 'ecookbook',
  206. :default_columns => '1',
  207. :fields => ["status_id", "assigned_to_id"],
  208. :operators => {
  209. "assigned_to_id" => "=", "status_id" => "o"
  210. },
  211. :values => {
  212. "assigned_to_id" => ["1"], "status_id" => ["1"]
  213. },
  214. :query => {
  215. "name" => "test_create_project_roles_query",
  216. "visibility" => "1",
  217. "role_ids" => ["1", "2", ""]
  218. }
  219. }
  220. )
  221. q = Query.find_by_name('test_create_project_roles_query')
  222. assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :query_id => q
  223. assert_equal Query::VISIBILITY_ROLES, q.visibility
  224. assert_equal [1, 2], q.roles.ids.sort
  225. end
  226. def test_create_global_private_query_with_custom_columns
  227. @request.session[:user_id] = 3
  228. post(
  229. :create,
  230. :params => {
  231. :fields => ["status_id", "assigned_to_id"],
  232. :operators => {
  233. "assigned_to_id" => "=", "status_id" => "o"
  234. },
  235. :values => {
  236. "assigned_to_id" => ["me"], "status_id" => ["1"]
  237. },
  238. :query => {
  239. "name" => "test_new_global_private_query", "visibility" => "0"
  240. },
  241. :c => ["", "tracker", "subject", "priority", "category"]
  242. }
  243. )
  244. q = Query.find_by_name('test_new_global_private_query')
  245. assert_redirected_to :controller => 'issues', :action => 'index', :project_id => nil, :query_id => q
  246. assert !q.is_public?
  247. assert !q.has_default_columns?
  248. assert_equal [:id, :tracker, :subject, :priority, :category], q.columns.collect {|c| c.name}
  249. assert q.valid?
  250. end
  251. def test_create_global_query_with_custom_filters
  252. @request.session[:user_id] = 3
  253. post(
  254. :create,
  255. :params => {
  256. :fields => ["assigned_to_id"],
  257. :operators => {
  258. "assigned_to_id" => "="
  259. },
  260. :values => {
  261. "assigned_to_id" => ["me"]
  262. },
  263. :query => {
  264. "name" => "test_new_global_query"
  265. }
  266. }
  267. )
  268. q = Query.find_by_name('test_new_global_query')
  269. assert_redirected_to :controller => 'issues', :action => 'index', :project_id => nil, :query_id => q
  270. assert !q.is_public?
  271. assert !q.has_filter?(:status_id)
  272. assert_equal ['assigned_to_id'], q.filters.keys
  273. assert q.valid?
  274. end
  275. def test_create_with_sort
  276. @request.session[:user_id] = 1
  277. post(
  278. :create,
  279. :params => {
  280. :default_columns => '1',
  281. :operators => {
  282. "status_id" => "o"
  283. },
  284. :values => {
  285. "status_id" => ["1"]
  286. },
  287. :query => {
  288. :name => "test_new_with_sort",
  289. :visibility => "2",
  290. :sort_criteria => {
  291. "0" => ["due_date", "desc"], "1" => ["tracker", ""]
  292. }
  293. }
  294. }
  295. )
  296. query = Query.find_by_name("test_new_with_sort")
  297. assert_not_nil query
  298. assert_equal [['due_date', 'desc'], ['tracker', 'asc']], query.sort_criteria
  299. end
  300. def test_create_with_failure
  301. @request.session[:user_id] = 2
  302. assert_no_difference '::Query.count' do
  303. post(
  304. :create,
  305. :params => {
  306. :project_id => 'ecookbook',
  307. :query => {
  308. :name => ''
  309. }
  310. }
  311. )
  312. end
  313. assert_response :success
  314. assert_select 'input[name=?]', 'query[name]'
  315. end
  316. def test_create_query_without_permission_should_fail
  317. Role.all.each {|r| r.remove_permission! :save_queries, :manage_public_queries}
  318. @request.session[:user_id] = 2
  319. assert_no_difference '::Query.count' do
  320. post(
  321. :create,
  322. :params => {
  323. :project_id => 'ecookbook',
  324. :query => {:name => 'Foo'}
  325. }
  326. )
  327. end
  328. assert_response 403
  329. end
  330. def test_create_global_query_without_permission_should_fail
  331. Role.all.each {|r| r.remove_permission! :save_queries, :manage_public_queries}
  332. @request.session[:user_id] = 2
  333. assert_no_difference '::Query.count' do
  334. post(:create, :params => {:query => {:name => 'Foo'}})
  335. end
  336. assert_response 403
  337. end
  338. def test_create_global_query_from_gantt
  339. @request.session[:user_id] = 1
  340. assert_difference 'IssueQuery.count' do
  341. post(
  342. :create,
  343. :params => {
  344. :gantt => 1,
  345. :operators => {
  346. "status_id" => "o"
  347. },
  348. :values => {
  349. "status_id" => ["1"]
  350. },
  351. :query => {
  352. :name => "test_create_from_gantt",
  353. :draw_relations => '1',
  354. :draw_progress_line => '1',
  355. :draw_selected_columns => '1'
  356. }
  357. }
  358. )
  359. assert_response 302
  360. end
  361. query = IssueQuery.order('id DESC').first
  362. assert_redirected_to "/issues/gantt?query_id=#{query.id}"
  363. assert_equal true, query.draw_relations
  364. assert_equal true, query.draw_progress_line
  365. assert_equal true, query.draw_selected_columns
  366. end
  367. def test_create_project_query_from_gantt
  368. @request.session[:user_id] = 1
  369. assert_difference 'IssueQuery.count' do
  370. post(
  371. :create,
  372. :params => {
  373. :project_id => 'ecookbook',
  374. :gantt => 1,
  375. :operators => {
  376. "status_id" => "o"
  377. },
  378. :values => {
  379. "status_id" => ["1"]
  380. },
  381. :query => {
  382. :name => "test_create_from_gantt",
  383. :draw_relations => '0',
  384. :draw_progress_line => '0',
  385. :draw_selected_columns => '0'
  386. }
  387. }
  388. )
  389. assert_response 302
  390. end
  391. query = IssueQuery.order('id DESC').first
  392. assert_redirected_to "/projects/ecookbook/issues/gantt?query_id=#{query.id}"
  393. assert_equal false, query.draw_relations
  394. assert_equal false, query.draw_progress_line
  395. assert_equal false, query.draw_selected_columns
  396. end
  397. def test_create_project_public_query_should_force_private_without_manage_public_queries_permission
  398. @request.session[:user_id] = 3
  399. query = new_record(Query) do
  400. post(
  401. :create,
  402. :params => {
  403. :project_id => 'ecookbook',
  404. :query => {
  405. "name" => "name", "visibility" => "2"
  406. }
  407. }
  408. )
  409. assert_response 302
  410. end
  411. assert_not_nil query.project
  412. assert_equal Query::VISIBILITY_PRIVATE, query.visibility
  413. end
  414. def test_create_global_public_query_should_force_private_without_manage_public_queries_permission
  415. @request.session[:user_id] = 3
  416. query = new_record(Query) do
  417. post(
  418. :create,
  419. :params => {
  420. :project_id => 'ecookbook',
  421. :query_is_for_all => '1',
  422. :query => {
  423. "name" => "name", "visibility" => "2"
  424. }
  425. }
  426. )
  427. assert_response 302
  428. end
  429. assert_nil query.project
  430. assert_equal Query::VISIBILITY_PRIVATE, query.visibility
  431. end
  432. def test_create_project_public_query_with_manage_public_queries_permission
  433. @request.session[:user_id] = 2
  434. query = new_record(Query) do
  435. post(
  436. :create,
  437. :params => {
  438. :project_id => 'ecookbook',
  439. :query => {
  440. "name" => "name", "visibility" => "2"
  441. }
  442. }
  443. )
  444. assert_response 302
  445. end
  446. assert_not_nil query.project
  447. assert_equal Query::VISIBILITY_PUBLIC, query.visibility
  448. end
  449. def test_create_global_public_query_should_force_private_with_manage_public_queries_permission
  450. @request.session[:user_id] = 2
  451. query = new_record(Query) do
  452. post(
  453. :create,
  454. :params => {
  455. :project_id => 'ecookbook',
  456. :query_is_for_all => '1',
  457. :query => {
  458. "name" => "name", "visibility" => "2"
  459. }
  460. }
  461. )
  462. assert_response 302
  463. end
  464. assert_nil query.project
  465. assert_equal Query::VISIBILITY_PRIVATE, query.visibility
  466. end
  467. def test_create_global_public_query_by_admin
  468. @request.session[:user_id] = 1
  469. query = new_record(Query) do
  470. post(
  471. :create,
  472. :params => {
  473. :project_id => 'ecookbook',
  474. :query_is_for_all => '1',
  475. :query => {
  476. "name" => "name", "visibility" => "2"
  477. }
  478. }
  479. )
  480. assert_response 302
  481. end
  482. assert_nil query.project
  483. assert_equal Query::VISIBILITY_PUBLIC, query.visibility
  484. end
  485. def test_create_project_public_time_entry_query
  486. @request.session[:user_id] = 2
  487. q = new_record(TimeEntryQuery) do
  488. post(
  489. :create,
  490. :params => {
  491. :project_id => 'ecookbook',
  492. :type => 'TimeEntryQuery',
  493. :default_columns => '1',
  494. :f => ["spent_on"],
  495. :op => {
  496. "spent_on" => "="
  497. },
  498. :v => {
  499. "spent_on" => ["2016-07-14"]
  500. },
  501. :query => {
  502. "name" => "test_new_project_public_query", "visibility" => "2"
  503. }
  504. }
  505. )
  506. end
  507. assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => 'ecookbook', :query_id => q.id
  508. assert q.is_public?
  509. assert q.has_default_columns?
  510. assert q.valid?
  511. end
  512. def test_create_public_project_query
  513. @request.session[:user_id] = 1
  514. q = new_record(ProjectQuery) do
  515. post(
  516. :create,
  517. :params => {
  518. :type => 'ProjectQuery',
  519. :default_columns => '1',
  520. :f => ["status"],
  521. :op => {
  522. "status" => "="
  523. },
  524. :v => {
  525. "status" => ['1']
  526. },
  527. :query => {
  528. "name" => "test_new_project_public_query", "visibility" => "2"
  529. }
  530. }
  531. )
  532. end
  533. assert_redirected_to :controller => 'projects', :action => 'index', :query_id => q.id
  534. assert q.is_public?
  535. assert q.valid?
  536. end
  537. def test_edit_global_public_query
  538. @request.session[:user_id] = 1
  539. get(:edit, :params => {:id => 4})
  540. assert_response :success
  541. assert_select 'input[name=?][value="2"][checked=checked]', 'query[visibility]'
  542. assert_select 'input[name=query_is_for_all][type=checkbox][checked=checked]'
  543. end
  544. def test_edit_global_private_query
  545. @request.session[:user_id] = 3
  546. get(:edit, :params => {:id => 3})
  547. assert_response :success
  548. assert_select 'input[name=?]', 'query[visibility]', 0
  549. assert_select 'input[name=query_is_for_all][type=checkbox][checked=checked]'
  550. end
  551. def test_edit_project_private_query
  552. @request.session[:user_id] = 3
  553. get(:edit, :params => {:id => 2})
  554. assert_response :success
  555. assert_select 'input[name=?]', 'query[visibility]', 0
  556. assert_select 'input[name=query_is_for_all][type=checkbox]:not([checked])'
  557. end
  558. def test_edit_project_public_query
  559. @request.session[:user_id] = 2
  560. get(:edit, :params => {:id => 1})
  561. assert_response :success
  562. assert_select 'input[name=?][value="2"][checked=checked]', 'query[visibility]'
  563. assert_select 'input[name=query_is_for_all][type=checkbox]:not([checked])'
  564. end
  565. def test_edit_sort_criteria
  566. @request.session[:user_id] = 1
  567. get(:edit, :params => {:id => 5})
  568. assert_response :success
  569. assert_select 'select[name=?]', 'query[sort_criteria][0][]' do
  570. assert_select 'option[value=priority][selected=selected]'
  571. assert_select 'option[value=desc][selected=selected]'
  572. end
  573. end
  574. def test_edit_invalid_query
  575. @request.session[:user_id] = 2
  576. get(:edit, :params => {:id => 99})
  577. assert_response 404
  578. end
  579. def test_update_global_private_query
  580. @request.session[:user_id] = 3
  581. put(
  582. :update,
  583. :params => {
  584. :id => 3,
  585. :default_columns => '1',
  586. :fields => ["status_id", "assigned_to_id"],
  587. :operators => {
  588. "assigned_to_id" => "=", "status_id" => "o"
  589. },
  590. :values => {
  591. "assigned_to_id" => ["me"], "status_id" => ["1"]
  592. },
  593. :query => {
  594. "name" => "test_edit_global_private_query", "visibility" => "2"
  595. }
  596. }
  597. )
  598. assert_redirected_to :controller => 'issues', :action => 'index', :query_id => 3
  599. q = Query.find_by_name('test_edit_global_private_query')
  600. assert !q.is_public?
  601. assert q.has_default_columns?
  602. assert q.valid?
  603. end
  604. def test_update_global_public_query
  605. @request.session[:user_id] = 1
  606. put(
  607. :update,
  608. :params => {
  609. :id => 4,
  610. :default_columns => '1',
  611. :fields => ["status_id", "assigned_to_id"],
  612. :operators => {
  613. "assigned_to_id" => "=", "status_id" => "o"
  614. },
  615. :values => {
  616. "assigned_to_id" => ["1"], "status_id" => ["1"]
  617. },
  618. :query => {
  619. "name" => "test_edit_global_public_query", "visibility" => "2"
  620. }
  621. }
  622. )
  623. assert_redirected_to :controller => 'issues', :action => 'index', :query_id => 4
  624. q = Query.find_by_name('test_edit_global_public_query')
  625. assert q.is_public?
  626. assert q.has_default_columns?
  627. assert q.valid?
  628. end
  629. def test_update_with_failure
  630. @request.session[:user_id] = 1
  631. put(
  632. :update,
  633. :params => {
  634. :id => 4,
  635. :query => {
  636. :name => ''
  637. }
  638. }
  639. )
  640. assert_response :success
  641. assert_select_error /Name cannot be blank/
  642. end
  643. def test_destroy
  644. @request.session[:user_id] = 2
  645. delete(:destroy, :params => {:id => 1})
  646. assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :set_filter => 1, :query_id => nil
  647. assert_nil Query.find_by_id(1)
  648. end
  649. def test_backslash_should_be_escaped_in_filters
  650. @request.session[:user_id] = 2
  651. get(:new, :params => {:subject => 'foo/bar'})
  652. assert_response :success
  653. assert_include 'addFilter("subject", "=", ["foo\/bar"]);', response.body
  654. end
  655. def test_filter_with_project_id_should_return_filter_values
  656. @request.session[:user_id] = 2
  657. get(
  658. :filter,
  659. :params => {
  660. :project_id => 1,
  661. :name => 'fixed_version_id'
  662. }
  663. )
  664. assert_response :success
  665. assert_equal 'application/json', response.media_type
  666. json = ActiveSupport::JSON.decode(response.body)
  667. assert_include ["eCookbook - 2.0", "3", "open"], json
  668. end
  669. def test_version_filter_time_entries_with_project_id_should_return_filter_values
  670. @request.session[:user_id] = 2
  671. get(
  672. :filter,
  673. :params => {
  674. :project_id => 1,
  675. :type => 'TimeEntryQuery',
  676. :name => 'issue.fixed_version_id'
  677. }
  678. )
  679. assert_response :success
  680. assert_equal 'application/json', response.media_type
  681. json = ActiveSupport::JSON.decode(response.body)
  682. assert_include ["eCookbook - 2.0", "3", "open"], json
  683. end
  684. def test_version_filter_without_project_id_should_return_all_visible_fixed_versions
  685. # Remove "jsmith" user from "Private child of eCookbook" project
  686. Project.find(5).memberships.find_by(:user_id => 2).destroy
  687. @request.session[:user_id] = 2
  688. get(
  689. :filter,
  690. :params => {
  691. :name => 'fixed_version_id'
  692. }
  693. )
  694. assert_response :success
  695. assert_equal 'application/json', response.media_type
  696. json = ActiveSupport::JSON.decode(response.body)
  697. # response includes visible version
  698. assert_include ["eCookbook Subproject 1 - 2.0", "4", "open"], json
  699. assert_include ["eCookbook - 0.1", "1", "closed"], json
  700. # response includes systemwide visible version
  701. assert_include ["OnlineStore - Systemwide visible version", "7", "open"], json
  702. # response doesn't include non visible version
  703. assert_not_include ["Private child of eCookbook - Private Version of public subproject", "6", "open"], json
  704. end
  705. def test_subproject_filter_time_entries_with_project_id_should_return_filter_values
  706. @request.session[:user_id] = 2
  707. get(
  708. :filter,
  709. :params => {
  710. :project_id => 1,
  711. :type => 'TimeEntryQuery',
  712. :name => 'subproject_id'
  713. }
  714. )
  715. assert_response :success
  716. assert_equal 'application/json', response.media_type
  717. json = ActiveSupport::JSON.decode(response.body)
  718. assert_equal 4, json.count
  719. assert_include ["Private child of eCookbook", "5"], json
  720. end
  721. def test_assignee_filter_should_return_active_and_locked_users_grouped_by_status
  722. @request.session[:user_id] = 1
  723. get(
  724. :filter,
  725. :params => {
  726. :project_id => 1,
  727. :type => 'IssueQuery',
  728. :name => 'assigned_to_id'
  729. }
  730. )
  731. assert_response :success
  732. assert_equal 'application/json', response.media_type
  733. json = ActiveSupport::JSON.decode(response.body)
  734. assert_equal 6, json.count
  735. # "me" value should not be grouped
  736. assert_include ["<< me >>", "me"], json
  737. assert_include ["Dave Lopper", "3", "active"], json
  738. assert_include ["Dave2 Lopper2", "5", "locked"], json
  739. end
  740. def test_author_filter_should_return_active_and_locked_users_grouped_by_status
  741. @request.session[:user_id] = 1
  742. get(
  743. :filter,
  744. :params => {
  745. :project_id => 1,
  746. :type => 'IssueQuery',
  747. :name => 'author_id'
  748. }
  749. )
  750. assert_response :success
  751. assert_equal 'application/json', response.media_type
  752. json = ActiveSupport::JSON.decode(response.body)
  753. assert_equal 7, json.count
  754. # "me" value should not be grouped
  755. assert_include ["<< me >>", "me"], json
  756. assert_include ["Dave Lopper", "3", "active"], json
  757. assert_include ["Dave2 Lopper2", "5", "locked"], json
  758. assert_include ["Anonymous", User.anonymous.id.to_s], json
  759. end
  760. def test_user_filter_should_return_active_and_locked_users_grouped_by_status
  761. @request.session[:user_id] = 1
  762. get(
  763. :filter,
  764. :params => {
  765. :project_id => 1,
  766. :type => 'TimeEntryQuery',
  767. :name => 'user_id'
  768. }
  769. )
  770. assert_response :success
  771. assert_equal 'application/json', response.media_type
  772. json = ActiveSupport::JSON.decode(response.body)
  773. assert_equal 7, json.count
  774. # "me" value should not be grouped
  775. assert_include ["<< me >>", "me"], json
  776. assert_include ["Dave Lopper", "3", "active"], json
  777. assert_include ["Dave2 Lopper2", "5", "locked"], json
  778. end
  779. def test_watcher_filter_without_permission_should_show_only_me
  780. # This user does not have view_issue_watchers permission
  781. @request.session[:user_id] = 7
  782. get(
  783. :filter,
  784. :params => {
  785. :project_id => 1,
  786. :type => 'IssueQuery',
  787. :name => 'watcher_id'
  788. }
  789. )
  790. assert_response :success
  791. assert_equal 'application/json', response.media_type
  792. json = ActiveSupport::JSON.decode(response.body)
  793. assert_equal 1, json.count
  794. assert_equal [["<< me >>", "me"]], json
  795. end
  796. def test_watcher_filter_with_permission_should_show_members_and_groups
  797. # This user has view_issue_watchers permission
  798. @request.session[:user_id] = 1
  799. get(
  800. :filter,
  801. :params => {
  802. :project_id => 1,
  803. :type => 'IssueQuery',
  804. :name => 'watcher_id'
  805. }
  806. )
  807. assert_response :success
  808. assert_equal 'application/json', response.media_type
  809. json = ActiveSupport::JSON.decode(response.body)
  810. assert_equal 7, json.count
  811. # "me" value should not be grouped
  812. assert_include ["<< me >>", "me"], json
  813. assert_include ["Dave Lopper", "3", "active"], json
  814. assert_include ["Dave2 Lopper2", "5", "locked"], json
  815. assert_include ["A Team", "10", "active"], json
  816. end
  817. def test_watcher_filter_with_permission_should_show_members_and_groups_globally
  818. # This user has view_issue_watchers permission
  819. @request.session[:user_id] = 1
  820. get(
  821. :filter,
  822. :params => {
  823. :type => 'IssueQuery',
  824. :name => 'watcher_id'
  825. }
  826. )
  827. assert_response :success
  828. assert_equal 'application/json', response.media_type
  829. json = ActiveSupport::JSON.decode(response.body)
  830. assert_equal 8, json.count
  831. # "me" value should not be grouped
  832. assert_include ['<< me >>', 'me'], json
  833. assert_include ['Dave Lopper', '3', 'active'], json
  834. assert_include ['Dave2 Lopper2', '5', 'locked'], json
  835. assert_include ['A Team', '10', 'active'], json
  836. end
  837. end