|
| 1 | +<?php |
| 2 | +/*========================================================================= |
| 3 | + MIDAS Server |
| 4 | + Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE |
| 5 | + All rights reserved. |
| 6 | + More information http://www.kitware.com |
| 7 | +
|
| 8 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + you may not use this file except in compliance with the License. |
| 10 | + You may obtain a copy of the License at |
| 11 | +
|
| 12 | + http://www.apache.org/licenses/LICENSE-2.0.txt |
| 13 | +
|
| 14 | + Unless required by applicable law or agreed to in writing, software |
| 15 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + See the License for the specific language governing permissions and |
| 18 | + limitations under the License. |
| 19 | +=========================================================================*/ |
| 20 | +/** test search controller*/ |
| 21 | +class SearchControllerTest extends ControllerTestCase |
| 22 | + { |
| 23 | + |
| 24 | + /** init test*/ |
| 25 | + public function setUp() |
| 26 | + { |
| 27 | + $this->setupDatabase(array('default')); |
| 28 | + $this->_models = array('User'); |
| 29 | + parent::setUp(); |
| 30 | + } |
| 31 | + |
| 32 | + /** Test the search results page */ |
| 33 | + public function testIndexAction() |
| 34 | + { |
| 35 | + $this->dispatchUrI('/search/?q=name'); |
| 36 | + $this->assertController('search'); |
| 37 | + $this->assertAction('index'); |
| 38 | + |
| 39 | + $this->resetAll(); |
| 40 | + $this->dispatchUrI('/search/?q=name&ajax'); |
| 41 | + $this->assertController('search'); |
| 42 | + $this->assertAction('index'); |
| 43 | + |
| 44 | + $resp = json_decode($this->getBody()); |
| 45 | + $this->assertEquals($resp->nitems, 0); |
| 46 | + $this->assertEquals($resp->nfolders, 2); |
| 47 | + $this->assertEquals($resp->ncommunities, 0); |
| 48 | + $this->assertEquals($resp->nusers, 2); |
| 49 | + |
| 50 | + $this->assertEquals(count($resp->results), 4); |
| 51 | + |
| 52 | + // First results should be the users |
| 53 | + $this->assertEquals($resp->results[0]->resultType, 'user'); |
| 54 | + $this->assertEquals($resp->results[1]->resultType, 'user'); |
| 55 | + $this->assertTrue(is_numeric($resp->results[0]->user_id)); |
| 56 | + $this->assertTrue(is_numeric($resp->results[1]->user_id)); |
| 57 | + $this->assertNotEmpty($resp->results[0]->firstname); |
| 58 | + $this->assertNotEmpty($resp->results[1]->firstname); |
| 59 | + $this->assertNotEmpty($resp->results[0]->lastname); |
| 60 | + $this->assertNotEmpty($resp->results[1]->lastname); |
| 61 | + |
| 62 | + // Next results should be the folders |
| 63 | + $this->assertEquals($resp->results[2]->resultType, 'folder'); |
| 64 | + $this->assertEquals($resp->results[3]->resultType, 'folder'); |
| 65 | + $this->assertTrue(is_numeric($resp->results[2]->folder_id)); |
| 66 | + $this->assertTrue(is_numeric($resp->results[3]->folder_id)); |
| 67 | + $this->assertNotEmpty($resp->results[2]->name); |
| 68 | + $this->assertNotEmpty($resp->results[3]->name); |
| 69 | + } |
| 70 | + |
| 71 | + /** Test the live search response */ |
| 72 | + public function testLiveSearch() |
| 73 | + { |
| 74 | + $this->dispatchUrI('/search/live?term=name'); |
| 75 | + $this->assertController('search'); |
| 76 | + $this->assertAction('live'); |
| 77 | + |
| 78 | + $resp = json_decode($this->getBody()); |
| 79 | + |
| 80 | + // Ensure we get item and user results from live search |
| 81 | + $this->assertEquals(count($resp), 4); |
| 82 | + $this->assertEquals($resp[0]->category, 'Folders'); |
| 83 | + $this->assertNotEmpty($resp[0]->value); |
| 84 | + $this->assertTrue(is_numeric($resp[0]->folderid)); |
| 85 | + $this->assertEquals($resp[1]->category, 'Folders'); |
| 86 | + $this->assertNotEmpty($resp[1]->value); |
| 87 | + $this->assertTrue(is_numeric($resp[1]->folderid)); |
| 88 | + $this->assertEquals($resp[2]->category, 'Users'); |
| 89 | + $this->assertNotEmpty($resp[2]->value); |
| 90 | + $this->assertTrue(is_numeric($resp[2]->userid)); |
| 91 | + $this->assertEquals($resp[3]->category, 'Users'); |
| 92 | + $this->assertNotEmpty($resp[3]->value); |
| 93 | + $this->assertTrue(is_numeric($resp[3]->userid)); |
| 94 | + |
| 95 | + // Ensure we get community results from live search |
| 96 | + $this->resetAll(); |
| 97 | + $this->dispatchUrI('/search/live?term=community'); |
| 98 | + $this->assertController('search'); |
| 99 | + $this->assertAction('live'); |
| 100 | + $resp = json_decode($this->getBody()); |
| 101 | + $this->assertEquals(count($resp), 1); |
| 102 | + $this->assertEquals($resp[0]->category, 'Communities'); |
| 103 | + $this->assertTrue(is_numeric($resp[0]->communityid)); |
| 104 | + $this->assertNotEmpty($resp[0]->label); |
| 105 | + $this->assertNotEmpty($resp[0]->value); |
| 106 | + |
| 107 | + // Ensure we get group results from live search with shareSearch enabled |
| 108 | + $this->resetAll(); |
| 109 | + $this->dispatchUrI('/search/live?term=community&shareSearch'); |
| 110 | + $this->assertController('search'); |
| 111 | + $this->assertAction('live'); |
| 112 | + $resp = json_decode($this->getBody()); |
| 113 | + $this->assertEquals(count($resp), 3); |
| 114 | + foreach($resp as $result) |
| 115 | + { |
| 116 | + $this->assertEquals($result->category, 'Groups'); |
| 117 | + $this->assertTrue(is_numeric($result->groupid)); |
| 118 | + $this->assertNotEmpty($result->label); |
| 119 | + $this->assertNotEmpty($result->value); |
| 120 | + } |
| 121 | + |
| 122 | + // Ensure we get only user results from live search with userSearch enabled |
| 123 | + $this->resetAll(); |
| 124 | + $this->dispatchUrI('/search/live?term=name&userSearch'); |
| 125 | + $this->assertController('search'); |
| 126 | + $this->assertAction('live'); |
| 127 | + $resp = json_decode($this->getBody()); |
| 128 | + $this->assertEquals(count($resp), 2); |
| 129 | + foreach($resp as $result) |
| 130 | + { |
| 131 | + $this->assertEquals($result->category, 'Users'); |
| 132 | + $this->assertTrue(is_numeric($result->userid)); |
| 133 | + $this->assertNotEmpty($result->label); |
| 134 | + $this->assertNotEmpty($result->value); |
| 135 | + } |
| 136 | + |
| 137 | + // Ensure we can't see items that we don't have read permissions on |
| 138 | + $this->resetAll(); |
| 139 | + $this->dispatchUrI('/search/live?term=name&itemSearch'); |
| 140 | + $this->assertController('search'); |
| 141 | + $this->assertAction('live'); |
| 142 | + $resp = json_decode($this->getBody()); |
| 143 | + $this->assertEquals(count($resp), 0); |
| 144 | + |
| 145 | + // Ensure we get item results from live search with itemSearch enabled if user has permissions |
| 146 | + $usersFile = $this->loadData('User', 'default'); |
| 147 | + $userDao = $this->User->load($usersFile[2]->getKey()); |
| 148 | + $this->resetAll(); |
| 149 | + $this->dispatchUrI('/search/live?term=name&itemSearch', $userDao); |
| 150 | + $this->assertController('search'); |
| 151 | + $this->assertAction('live'); |
| 152 | + $resp = json_decode($this->getBody()); |
| 153 | + |
| 154 | + $this->assertEquals(count($resp), 1); |
| 155 | + foreach($resp as $result) |
| 156 | + { |
| 157 | + $this->assertEquals($result->category, 'Items'); |
| 158 | + $this->assertTrue(is_numeric($result->itemid)); |
| 159 | + $this->assertNotEmpty($result->label); |
| 160 | + $this->assertNotEmpty($result->value); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
0 commit comments