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

Commit 9a0ca38

Browse files
author
Charles Marion
committed
ENH: added first controller tests
1 parent 6c2b5fd commit 9a0ca38

File tree

14 files changed

+307
-80
lines changed

14 files changed

+307
-80
lines changed

core/AppController.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public function preDispatch()
1616
parent::preDispatch();
1717
$this->view->setEncoding('iso-8859-1');
1818

19+
$this->view->setScriptPath(BASE_PATH."/core/views");
20+
1921
$fc = Zend_Controller_Front::getInstance();
2022
$module = $fc->getRequest()->getModuleName();
2123
if($module == 'default')
@@ -51,8 +53,25 @@ public function preDispatch()
5153
Zend_Session::setId($_POST['sid']);
5254
}
5355
Zend_Session::start();
54-
$user = new Zend_Session_Namespace('Auth_User');
56+
57+
// log in when testing
58+
$testingUserId = $this->_getParam('testingUserId');
5559
$modelLoad = new MIDAS_ModelLoader();
60+
if(Zend_Registry::get('configGlobal')->environment == 'testing' && isset($testingUserId))
61+
{
62+
$user = new Zend_Session_Namespace('Auth_User');
63+
$userModel = $modelLoad->loadModel('User');
64+
$user->Dao = $userModel->load($testingUserId);
65+
if($user->Dao == false)
66+
{
67+
throw new Zend_Exception('Unable to find user');
68+
}
69+
}
70+
else
71+
{
72+
$user = new Zend_Session_Namespace('Auth_User');
73+
}
74+
5675
if($user->Dao == null)
5776
{
5877
$userModel = $modelLoad->loadModel('User');
@@ -222,6 +241,26 @@ function getServerURL()
222241
return $prefix.$_SERVER['SERVER_NAME'].$currentPort;
223242
}
224243

244+
/** check if testing environement is set */
245+
public function isTestingEnv()
246+
{
247+
return Zend_Registry::get('configGlobal')->environment == 'testing';
248+
}
249+
250+
/** disable layout */
251+
public function disableLayout()
252+
{
253+
if($this->_helper->hasHelper('layout'))
254+
{
255+
$this->_helper->layout->disableLayout();
256+
}
257+
}
258+
/** disable view */
259+
public function disableView()
260+
{
261+
$this->_helper->viewRenderer->setNoRender();
262+
}
263+
225264
/** check if midas needs to be upgraded */
226265
public function isUpgradeNeeded()
227266
{
@@ -254,7 +293,7 @@ public function postDispatch()
254293
parent::postDispatch();
255294
$this->view->json = JsonComponent::encode($this->view->json);
256295
$this->view->generatedTimer = round((microtime(true) - START_TIME), 3);
257-
if(Zend_Registry::get('config')->environment != 'testing')
296+
if(Zend_Registry::get('configGlobal')->environment != 'testing')
258297
{
259298
header('Content-Type: text/html; charset=ISO-8859-1');
260299
}

core/configs/core.ini

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ bootstrap.path = "./core/Bootstrap.php"
44
bootstrap.class = "Bootstrap"
55
appnamespace = "Midas"
66
resources.frontController.controllerDirectory = "./core/controllers"
7+
resources.frontController.moduleDirectory = "./modules"
78
resources.layout.layoutPath ="./core/layouts/"
89
resources.view.scriptPath = "./core/views/"
9-
resources.frontController.moduleDirectory = "./modules"
1010

1111
[testing]
12-
resources.frontController.controllerDirectory = "./../core/controllers"
13-
resources.frontController.moduleDirectory = "./../modules"
14-
resources.layout.layoutPath ="./../core/layouts/"
15-
resources.view.scriptPath = "./../core/views/";

core/controllers/ErrorController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ private function _applicationError()
102102
$this->view->message = $shortMessage;
103103
break;
104104
case 'testing':
105-
$this->_helper->layout->setLayout('blank');
105+
if($this->_helper->hasHelper('layout'))
106+
{
107+
$this->_helper->layout->disableLayout();
108+
}
109+
106110
$this->_helper->viewRenderer->setNoRender();
107111

108112
$this->getResponse()->appendBody($shortMessage);

core/controllers/FeedController.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function indexAction()
2626
$this->view->notifications = array();
2727
$this->view->header = $this->t('Feed');
2828

29-
if($this->logged)
29+
if($this->logged && !$this->isTestingEnv())
3030
{
3131
$request = $this->getRequest();
3232
$cookieData = $request->getCookie('newFeed'.$this->userSession->Dao->getKey());
@@ -38,33 +38,35 @@ public function indexAction()
3838
}
3939
}
4040

41-
/** get getfolders Items' size */
41+
/** get delete a feed */
4242
public function deleteajaxAction()
4343
{
44-
if(!$this->getRequest()->isXmlHttpRequest())
44+
if(!$this->getRequest()->isXmlHttpRequest() && !$this->isTestingEnv())
4545
{
4646
throw new Zend_Exception("Why are you here ? Should be ajax.");
4747
}
4848

49-
$this->_helper->layout->disableLayout();
50-
$this->_helper->viewRenderer->setNoRender();
49+
$this->disableLayout();
50+
$this->disableView();
5151

5252
$feedId = $this->_getParam('feed');
5353
if(!isset($feedId) || (!is_numeric($feedId) && strlen($feedId) != 32)) // This is tricky! and for Cassandra for now)
5454
{
5555
throw new Zend_Exception("Please set the feed Id");
5656
}
5757
$feed = $this->Feed->load($feedId);
58+
5859
if($feed == false)
5960
{
6061
return;
6162
}
62-
if(!$this->Feed->policyCheck($feed, $this->userSession->Dao, 2))
63+
64+
if(!$this->Feed->policyCheck($feed, $this->userSession->Dao, MIDAS_POLICY_ADMIN))
6365
{
6466
return;
6567
}
6668
$this->Feed->delete($feed);
67-
}//end getfolderscontent
69+
}//end deleteajaxAction
6870

6971
} // end class
7072

core/models/base/FeedModelBase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function __construct()
2727
protected abstract function getFeeds($loggedUserDao, $userDao = null, $communityDao = null, $policy = 0, $limit = 20);
2828
/** add a community*/
2929
protected abstract function addCommunity($feed, $community);
30+
abstract function policyCheck($feedDao, $userDao = null, $policy = 0);
3031

3132

3233
/** get feeds (filtered by policies)

core/models/pdo/FeedModel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ function policyCheck($feedDao, $userDao = null, $policy = 0)
8888

8989
$sql = $this->database->select()
9090
->union(array($subqueryUser, $subqueryGroup));
91+
9192
$rowset = $this->database->fetchAll($sql);
9293
if(count($rowset) > 0)
9394
{

core/views/element/feed.phtml

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,62 @@
55
?>
66

77
<?
8+
9+
10+
if(!function_exists('createFeedElement'))
11+
{
12+
function createFeedElement(&$view,$feed)
13+
{
14+
$type=$feed->getType();
15+
if($feed->policy==2)
16+
{
17+
echo "<div class='feedDelete'>";
18+
echo "<img class='feedDeleteLink' element='{$feed->getKey()}' src='{$view->coreWebroot}/public/images/icons/close.png' alt=''/>";
19+
echo "</div>";
20+
}
21+
echo "<div class='feedUserImage'>";
22+
$thumbnail=$feed->getUser()->getThumbnail();
23+
24+
echo $view->userthumbnail($thumbnail);
25+
echo "</div>";
26+
echo "<div class='feedInfo'>";
27+
echo $view->linkuser($feed->getUser()).' ';
28+
switch($type)
29+
{
30+
case MIDAS_FEED_CREATE_COMMUNITY:
31+
echo "{$view->t("added the community")} <a href='{$view->webroot}/community/{$feed->getRessource()->getKey()}'>".$view->slicename($feed->getRessource()->getName(),30)."</a>";
32+
break;
33+
case MIDAS_FEED_COMMUNITY_INVITATION:
34+
echo "{$view->t("invited you to the community")} <a href='{$view->webroot}/community/{$feed->getRessource()->getCommunity()->getKey()}'>".$view->slicename($feed->getRessource()->getCommunity()->getName(),30)."</a>";
35+
break;
36+
case MIDAS_FEED_UPDATE_COMMUNITY:
37+
case MIDAS_FEED_CREATE_FOLDER:
38+
case MIDAS_FEED_CREATE_ITEM:
39+
echo "{$view->t("added the item")} <a href='{$view->webroot}/item/{$feed->getRessource()->getKey()}'>".$view->slicename($feed->getRessource()->getName(),30)."</a>";
40+
break;
41+
case MIDAS_FEED_CREATE_LINK_ITEM:
42+
echo "{$view->t("added the link")} <a href='{$view->webroot}/item/{$feed->getRessource()->getKey()}'>".$view->slicename($feed->getRessource()->getName(),30)."</a>";
43+
break;
44+
case MIDAS_FEED_CREATE_REVISION:
45+
echo "{$view->t("added a new revision to")} <a href='{$view->webroot}/item/{$feed->getRessource()->getItem()->getKey()}'>".$view->slicename($feed->getRessource()->getItem()->getName(),30)."</a>";
46+
break;
47+
case MIDAS_FEED_CREATE_USER:
48+
echo "{$view->t("registered")}";
49+
break;
50+
case MIDAS_FEED_DELETE_COMMUNITY:
51+
case MIDAS_FEED_DELETE_FOLDER:
52+
case MIDAS_FEED_DELETE_ITEM:
53+
default:
54+
break;
55+
}
56+
echo "</div>";
57+
echo "<div class='feedDate'>";
58+
echo $view->Dateago(strtotime($feed->getDate()));
59+
echo "</div>";
60+
echo "<div style='clear:left;'></div>";
61+
}
62+
}
63+
864
$feeds=$this->feeds;
965
if(!isset($feeds)||empty($feeds))
1066
{
@@ -24,56 +80,5 @@
2480
}
2581
echo "</div>";
2682

27-
function createFeedElement(&$view,$feed)
28-
{
29-
$type=$feed->getType();
30-
if($feed->policy==2)
31-
{
32-
echo "<div class='feedDelete'>";
33-
echo "<img class='feedDeleteLink' element='{$feed->getKey()}' src='{$view->coreWebroot}/public/images/icons/close.png' alt=''/>";
34-
echo "</div>";
35-
}
36-
echo "<div class='feedUserImage'>";
37-
$thumbnail=$feed->getUser()->getThumbnail();
38-
39-
echo $view->userthumbnail($thumbnail);
40-
echo "</div>";
41-
echo "<div class='feedInfo'>";
42-
echo $view->linkuser($feed->getUser()).' ';
43-
switch($type)
44-
{
45-
case MIDAS_FEED_CREATE_COMMUNITY:
46-
echo "{$view->t("added the community")} <a href='{$view->webroot}/community/{$feed->getRessource()->getKey()}'>".$view->slicename($feed->getRessource()->getName(),30)."</a>";
47-
break;
48-
case MIDAS_FEED_COMMUNITY_INVITATION:
49-
echo "{$view->t("invited you to the community")} <a href='{$view->webroot}/community/{$feed->getRessource()->getCommunity()->getKey()}'>".$view->slicename($feed->getRessource()->getCommunity()->getName(),30)."</a>";
50-
break;
51-
case MIDAS_FEED_UPDATE_COMMUNITY:
52-
case MIDAS_FEED_CREATE_FOLDER:
53-
case MIDAS_FEED_CREATE_ITEM:
54-
echo "{$view->t("added the item")} <a href='{$view->webroot}/item/{$feed->getRessource()->getKey()}'>".$view->slicename($feed->getRessource()->getName(),30)."</a>";
55-
break;
56-
case MIDAS_FEED_CREATE_LINK_ITEM:
57-
echo "{$view->t("added the link")} <a href='{$view->webroot}/item/{$feed->getRessource()->getKey()}'>".$view->slicename($feed->getRessource()->getName(),30)."</a>";
58-
break;
59-
case MIDAS_FEED_CREATE_REVISION:
60-
echo "{$view->t("added a new revision to")} <a href='{$view->webroot}/item/{$feed->getRessource()->getItem()->getKey()}'>".$view->slicename($feed->getRessource()->getItem()->getName(),30)."</a>";
61-
break;
62-
case MIDAS_FEED_CREATE_USER:
63-
echo "{$view->t("registered")}";
64-
break;
65-
case MIDAS_FEED_DELETE_COMMUNITY:
66-
case MIDAS_FEED_DELETE_FOLDER:
67-
case MIDAS_FEED_DELETE_ITEM:
68-
default:
69-
break;
70-
}
71-
echo "</div>";
72-
echo "<div class='feedDate'>";
73-
echo $view->Dateago(strtotime($feed->getDate()));
74-
echo "</div>";
75-
echo "<div style='clear:left;'></div>";
76-
}
77-
7883
?>
7984

tests/bootstrap.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
Zend_Registry::set('logger', null);
3434

3535

36-
$configGlobal = new Zend_Config_Ini(APPLICATION_CONFIG, 'global');
36+
$configGlobal = new Zend_Config_Ini(APPLICATION_CONFIG, 'global', true);
37+
$configGlobal->environment = 'testing';
3738
Zend_Registry::set('configGlobal', $configGlobal);
3839

3940
$config = new Zend_Config_Ini(APPLICATION_CONFIG, 'testing');

0 commit comments

Comments
 (0)