Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 38f3809

Browse files
author
Michael Grauer
committed
BUG: refs #0430. Added test and small changes to ease testing.
1 parent 8615719 commit 38f3809

File tree

3 files changed

+336
-11
lines changed

3 files changed

+336
-11
lines changed

core/controllers/AssetstoreController.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ function indexAction()
5353
function defaultassetstoreAction()
5454
{
5555
$this->requireAdminPrivileges();
56-
$this->_helper->layout->disableLayout();
57-
$this->_helper->viewRenderer->setNoRender();
56+
$this->disableLayout();
57+
$this->disableView();
5858
$change = $this->_getParam("submitDefaultAssetstore");
5959
$element = $this->_getParam("element");
60-
if(isset($change))
60+
if(isset($change) && isset($element))
6161
{
6262
$assetstore = $this->Assetstore->load($element);
6363
if($assetstore != false)
6464
{
65-
$this->Setting->setConfig('default_assetstore', $assetstore->getKey());
65+
$this->Setting->setConfig('default_assetstore', (string)$assetstore->getKey());
6666
echo JsonComponent::encode(array(true, $this->t('Changes saved')));
6767
return;
6868
}
@@ -75,8 +75,8 @@ function defaultassetstoreAction()
7575
function deleteAction()
7676
{
7777
$this->requireAdminPrivileges();
78-
$this->_helper->layout->disableLayout();
79-
$this->_helper->viewRenderer->setNoRender();
78+
$this->disableLayout();
79+
$this->disableView();
8080
$assetstoreId = $this->_getParam("assetstoreId");
8181
if(isset($assetstoreId))
8282
{
@@ -96,8 +96,8 @@ function deleteAction()
9696
function editAction()
9797
{
9898
$this->requireAdminPrivileges();
99-
$this->_helper->layout->disableLayout();
100-
$this->_helper->viewRenderer->setNoRender();
99+
$this->disableLayout();
100+
$this->disableView();
101101
$assetstoreId = $this->_getParam("assetstoreId");
102102
$assetstoreName = $this->_getParam("assetstoreName");
103103
$assetstorePath = $this->_getParam("assetstorePath");
@@ -131,8 +131,8 @@ function editAction()
131131
function addAction()
132132
{
133133
$this->requireAdminPrivileges();
134-
$this->_helper->layout->disableLayout();
135-
$this->_helper->viewRenderer->setNoRender();
134+
$this->disableLayout();
135+
$this->disableView();
136136

137137
$form = $this->Form->Assetstore->createAssetstoreForm();
138138
if($this->getRequest()->isPost() && !$form->isValid($_POST))
Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
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 assetstore controller*/
21+
class AssetstoreControllerTest extends ControllerTestCase
22+
{
23+
24+
protected $nullUserDao;
25+
protected $nonAdminUserDao;
26+
protected $AdminUserDao;
27+
protected $testAssetstoreDao;
28+
protected $testAssetstoreAdditionalPath;
29+
30+
/** init tests*/
31+
public function setUp()
32+
{
33+
$this->setupDatabase(array('default'));
34+
$this->_models = array('Assetstore', 'User');
35+
$this->_daos = array('Assetstore', 'User');
36+
parent::setUp();
37+
$this->loadUsers();
38+
39+
// create another assetstore
40+
$testAssetstoreBase = $this->getTempDirectory().'/test/';
41+
$testAssetstoreBase = str_replace('tests/../', '', $testAssetstoreBase);
42+
$testAssetstoreBase = str_replace('//', '/', $testAssetstoreBase);
43+
$testAssetstore2 = $testAssetstoreBase . '/assetstore2';
44+
if(!is_dir($testAssetstore2))
45+
{
46+
mkdir($testAssetstore2);
47+
}
48+
$testAssetstoreAdditionalPath = $testAssetstoreBase . '/additionalpathassetstore2';
49+
if(!is_dir($testAssetstoreAdditionalPath))
50+
{
51+
mkdir($testAssetstoreAdditionalPath);
52+
}
53+
$this->testAssetstoreAdditionalPath = $testAssetstoreAdditionalPath;
54+
55+
$testAssetstoreDao = new AssetstoreDao();
56+
$testAssetstoreDao->setName('testassetstore2');
57+
$testAssetstoreDao->setPath($testAssetstore2);
58+
$testAssetstoreDao->setType(MIDAS_ASSETSTORE_LOCAL);
59+
$this->Assetstore->save($testAssetstoreDao);
60+
$this->testAssetstoreDao = $testAssetstoreDao;
61+
}
62+
63+
/** tearDown tester method. */
64+
public function tearDown()
65+
{
66+
$this->Assetstore->delete($this->testAssetstoreDao);
67+
}
68+
69+
/** helper method, load the 3 different user daos. */
70+
protected function loadUsers()
71+
{
72+
$usersFile = $this->loadData('User', 'default');
73+
$this->nullUserDao = null;
74+
foreach($usersFile as $userDao)
75+
{
76+
if($userDao->getFirstname() === 'Admin')
77+
{
78+
$this->adminUserDao = $userDao;
79+
}
80+
else if($userDao->getFirstname() === 'FirstName1')
81+
{
82+
$this->nonAdminUserDao = $userDao;
83+
}
84+
}
85+
}
86+
87+
/** helper method, ensures only admins can call the action. */
88+
protected function ensureAdminRequired($pageURI)
89+
{
90+
// first try to bring up the page without logging in, should get an exception
91+
$withException = true;
92+
$this->params = array();
93+
$this->resetAll();
94+
$this->getRequest()->setMethod('POST');
95+
$this->dispatchUrI($pageURI, $this->nullUserDao, $withException);
96+
97+
// now login with a non-admin account, should get an exception
98+
$this->resetAll();
99+
$this->params = array();
100+
$this->getRequest()->setMethod('POST');
101+
$this->dispatchUrI($pageURI, $this->nonAdminUserDao, $withException);
102+
103+
// now login with an admin account
104+
$this->resetAll();
105+
$this->params = array();
106+
$this->getRequest()->setMethod('POST');
107+
$this->dispatchUrI($pageURI, $this->adminUserDao);
108+
}
109+
110+
/** test defaultassetstore action */
111+
function testDefaultassetstoreAction()
112+
{
113+
$pageURI = '/assetstore/defaultassetstore';
114+
$this->ensureAdminRequired($pageURI);
115+
116+
// get the default assetstore
117+
$initialDefaultAssetstoreDao = $this->Assetstore->getDefault();
118+
119+
// set the second assetstore as default
120+
$this->resetAll();
121+
$this->params = array();
122+
$this->params['submitDefaultAssetstore'] = 'submitDefaultAssetstore';
123+
$this->params['element'] = $this->testAssetstoreDao->getKey();
124+
$this->getRequest()->setMethod('POST');
125+
$this->dispatchUrI($pageURI, $this->adminUserDao);
126+
$response = json_decode($this->getBody());
127+
$this->assertEquals(1, $response[0], "Expected true json response");
128+
129+
$defaultAssetstoreDao = $this->Assetstore->getDefault();
130+
131+
$juggleTypes = true;
132+
$this->assertTrue($this->Assetstore->compareDao($defaultAssetstoreDao, $this->testAssetstoreDao, $juggleTypes), 'New default assetstore is not the real default assetstore');
133+
134+
// now set back to the original default
135+
$this->resetAll();
136+
$this->params = array();
137+
$this->params['submitDefaultAssetstore'] = 'submitDefaultAssetstore';
138+
$this->params['element'] = $initialDefaultAssetstoreDao->getKey();
139+
$this->getRequest()->setMethod('POST');
140+
$this->dispatchUrI($pageURI, $this->adminUserDao);
141+
$response = json_decode($this->getBody());
142+
$this->assertEquals(1, $response[0], "Expected true json response");
143+
144+
$defaultAssetstoreDao = $this->Assetstore->getDefault();
145+
$this->assertTrue($this->Assetstore->compareDao($defaultAssetstoreDao, $initialDefaultAssetstoreDao, $juggleTypes), 'New default assetstore is not the real default assetstore');
146+
147+
// now don't send a submitDefaultAssetstore param and be sure we get an error
148+
$this->resetAll();
149+
$this->params = array();
150+
$this->getRequest()->setMethod('POST');
151+
$this->dispatchUrI($pageURI, $this->adminUserDao);
152+
$response = json_decode($this->getBody());
153+
$this->assertEquals("", $response[0], "Expected false json response");
154+
155+
// now don't send a submitDefaultAssetstore param and be sure we get an error
156+
$this->resetAll();
157+
$this->params = array();
158+
$this->params['submitDefaultAssetstore'] = 'submitDefaultAssetstore';
159+
//$this->params['element'] = $initialDefaultAssetstoreDao->getKey();
160+
$this->getRequest()->setMethod('POST');
161+
$this->dispatchUrI($pageURI, $this->adminUserDao);
162+
$response = json_decode($this->getBody());
163+
$this->assertEquals("", $response[0], "Expected false json response");
164+
}
165+
166+
/** test delete action */
167+
function testDeleteAction()
168+
{
169+
$pageURI = '/assetstore/delete';
170+
$this->ensureAdminRequired($pageURI);
171+
172+
$testAssetstoreId = $this->testAssetstoreDao->getKey();
173+
174+
// delete the assetstore via the controller
175+
$this->resetAll();
176+
$this->params = array();
177+
$this->params['assetstoreId'] = $testAssetstoreId;
178+
$this->getRequest()->setMethod('POST');
179+
$this->dispatchUrI($pageURI, $this->adminUserDao);
180+
$response = json_decode($this->getBody());
181+
$this->assertEquals(1, $response[0], "Expected true json response");
182+
183+
// try to load and be sure it is deleted
184+
$testAssetstoreDao = $this->Assetstore->load($testAssetstoreId);
185+
$this->assertFalse($testAssetstoreDao);
186+
}
187+
188+
189+
/** helper method, sends a request to assetstore controller */
190+
protected function dispatchRequestJson($pageURI, $params)
191+
{
192+
$this->resetAll();
193+
$this->params = $params;
194+
$this->getRequest()->setMethod('POST');
195+
$this->dispatchUrI($pageURI, $this->adminUserDao);
196+
$response = json_decode($this->getBody());
197+
return $response;
198+
}
199+
200+
/** helper method, expects a false response */
201+
protected function expectFalseJson($pageURI, $params)
202+
{
203+
$response = $this->dispatchRequestJson($pageURI, $params);
204+
$this->assertEquals("", $response[0], "Expected false json response");
205+
}
206+
207+
/** helper method, expects a true response */
208+
protected function expectTrueJson($pageURI, $params)
209+
{
210+
$response = $this->dispatchRequestJson($pageURI, $params);
211+
$this->assertEquals(1, $response[0], "Expected true json response");
212+
return $response;
213+
}
214+
215+
/** helper method, expects an 'error' key */
216+
protected function expectErrorJson($pageURI, $params)
217+
{
218+
$response = $this->dispatchRequestJson($pageURI, $params);
219+
$this->assertTrue(isset($response->error));
220+
}
221+
222+
223+
224+
/** test edit action */
225+
function testEditAction()
226+
{
227+
$pageURI = '/assetstore/edit';
228+
$this->ensureAdminRequired($pageURI);
229+
230+
$testAssetstoreId = $this->testAssetstoreDao->getKey();
231+
$testAssetstoreName = $this->testAssetstoreDao->getName();
232+
$testAssetstorePath = $this->testAssetstoreDao->getPath();
233+
$testAssetstoreNewName = "anewname";
234+
235+
// get the default assetstore
236+
$defaultAssetstoreDao = $this->Assetstore->getDefault();
237+
$defaultAssetstoreName = $defaultAssetstoreDao->getName();
238+
$defaultAssetstorePath = $defaultAssetstoreDao->getPath();
239+
240+
// test error conditions first
241+
242+
// don't send ID
243+
$params = array();
244+
$this->expectFalseJson($pageURI, $params);
245+
246+
// don't send name
247+
$params = array("assetstoreId" => $testAssetstoreId);
248+
$this->expectFalseJson($pageURI, $params);
249+
250+
// don't send path
251+
$params = array("assetstoreId" => $testAssetstoreId, "assetstoreName" => $testAssetstoreName);
252+
$this->expectFalseJson($pageURI, $params);
253+
254+
// send a bad path
255+
$params = array("assetstoreId" => $testAssetstoreId, "assetstoreName" => $testAssetstoreName, "assetstorePath" => '/this/path/probably/will/not/exist');
256+
$this->expectFalseJson($pageURI, $params);
257+
258+
// try to edit to same name as default
259+
$params = array("assetstoreId" => $testAssetstoreId, "assetstoreName" => $defaultAssetstoreName, "assetstorePath" => $testAssetstorePath);
260+
$this->expectFalseJson($pageURI, $params);
261+
262+
// try to edit to same path as default
263+
$params = array("assetstoreId" => $testAssetstoreId, "assetstoreName" => $testAssetstoreName, "assetstorePath" => $defaultAssetstorePath);
264+
$this->expectFalseJson($pageURI, $params);
265+
266+
// edit name
267+
$params = array("assetstoreId" => $testAssetstoreId, "assetstoreName" => $testAssetstoreNewName, "assetstorePath" => $testAssetstorePath);
268+
$this->expectTrueJson($pageURI, $params);
269+
$updatedTestAssetstore = $this->Assetstore->load($testAssetstoreId);
270+
$this->assertEquals($updatedTestAssetstore->getName(), $testAssetstoreNewName);
271+
272+
// edit path
273+
$params = array("assetstoreId" => $testAssetstoreId, "assetstoreName" => $testAssetstoreNewName, "assetstorePath" => $this->testAssetstoreAdditionalPath);
274+
$this->expectTrueJson($pageURI, $params);
275+
$updatedTestAssetstore = $this->Assetstore->load($testAssetstoreId);
276+
$this->assertEquals($updatedTestAssetstore->getPath(), $this->testAssetstoreAdditionalPath);
277+
278+
// edit name and path
279+
$params = array("assetstoreId" => $testAssetstoreId, "assetstoreName" => $testAssetstoreName, "assetstorePath" => $testAssetstorePath);
280+
$this->expectTrueJson($pageURI, $params);
281+
$updatedTestAssetstore = $this->Assetstore->load($testAssetstoreId);
282+
$this->assertEquals($updatedTestAssetstore->getName(), $testAssetstoreName);
283+
$this->assertEquals($updatedTestAssetstore->getPath(), $testAssetstorePath);
284+
}
285+
286+
287+
288+
/** test add action */
289+
function testAddAction()
290+
{
291+
$pageURI = '/assetstore/add';
292+
$this->ensureAdminRequired($pageURI);
293+
294+
// get the default assetstore
295+
$defaultAssetstoreDao = $this->Assetstore->getDefault();
296+
$defaultAssetstoreName = $defaultAssetstoreDao->getName();
297+
$defaultAssetstorePath = $defaultAssetstoreDao->getPath();
298+
299+
$newAssetstoreName = "anewname";
300+
301+
// test error conditions first
302+
303+
// try to add as same name as default
304+
$params = array("name" => $defaultAssetstoreName, "basedirectory" => $this->testAssetstoreAdditionalPath, "assetstoretype" => '0');
305+
$this->expectErrorJson($pageURI, $params);
306+
307+
// try to add as same path as default
308+
$params = array("name" => $newAssetstoreName, "basedirectory" => $defaultAssetstorePath, "assetstoretype" => '0');
309+
$this->expectErrorJson($pageURI, $params);
310+
311+
// add and check saved values
312+
$params = array("name" => $newAssetstoreName, "basedirectory" => $this->testAssetstoreAdditionalPath, "assetstoretype" => '1');
313+
$response = $this->dispatchRequestJson($pageURI, $params);//$this->expectTrueJson($pageURI, $params);
314+
$this->assertTrue(isset($response->assetstore_id), "Expected error key assetstore_id in response");
315+
$createdAssetstoreDao = $this->Assetstore->load($response->assetstore_id);
316+
$this->assertEquals($createdAssetstoreDao->getName(), $newAssetstoreName);
317+
$this->assertEquals($createdAssetstoreDao->getPath(), $this->testAssetstoreAdditionalPath);
318+
$this->assertEquals($createdAssetstoreDao->getType(), '1');
319+
320+
// delete the newly added assetstore
321+
$this->Assetstore->delete($createdAssetstoreDao);
322+
}
323+
324+
}

core/tests/controllers/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
add_midas_test( AssetstoreController AssetstoreControllerTest.php )
12
add_midas_test( BrowseController BrowseControllerTest.php )
23
add_midas_test( FeedController FeedControllerTest.php )
34
add_midas_test( ItemController ItemControllerTest.php )
@@ -7,7 +8,7 @@ add_midas_test( UserController UserControllerTest.php )
78
add_subdirectory( components )
89

910

10-
# Syle
11+
# Style
1112
add_midas_style_test( StyleCoreControllerComponents ${CMAKE_SOURCE_DIR}/core/controllers/components )
1213
add_midas_style_test( StyleCoreControllerForms ${CMAKE_SOURCE_DIR}/core/controllers/forms )
1314
add_midas_style_test( StyleCoreAdminController ${CMAKE_SOURCE_DIR}/core/controllers/AdminController.php )

0 commit comments

Comments
 (0)