Skip to content

Commit

Permalink
Merge pull request #2 from biblibre/module-controller-test
Browse files Browse the repository at this point in the history
Module controller test
  • Loading branch information
stl74 committed May 26, 2016
2 parents 5703ed7 + 8f07db9 commit 650862a
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 5 deletions.
1 change: 1 addition & 0 deletions application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
'itemSetSelect' => 'Omeka\View\Helper\ItemSetSelect',
'formPropertyInputs' => 'Omeka\View\Helper\PropertyInputs',
'resourceClassSelect' => 'Omeka\View\Helper\ResourceClassSelect',
'siteSelect' => 'Omeka\View\Helper\SiteSelect',
'deleteConfirm' => 'Omeka\View\Helper\DeleteConfirm',
'searchFilters' => 'Omeka\View\Helper\SearchFilters',
'ckEditor' => 'Omeka\View\Helper\CkEditor',
Expand Down
2 changes: 1 addition & 1 deletion application/src/Service/ModuleManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ModuleManagerFactory implements FactoryInterface
{
/**
* Create the module manager
*
*
* @param ServiceLocatorInterface $serviceLocator
* @return array
*/
Expand Down
3 changes: 2 additions & 1 deletion application/src/Settings/SiteSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ public function setSite($site)
} else {
$this->siteId = null;
}
$this->cache=null;
}

protected function setCache()
{
if (!$this->siteId) {
if (!$this->siteId) {
throw new Exception\RuntimeException('Cannot use site settings when no site is set');
}
$conn = $this->getConnection();
Expand Down
111 changes: 110 additions & 1 deletion application/src/Test/AbstractHttpControllerTestCase.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
<?php

namespace Omeka\Test;

use Omeka\Entity\User;
use Omeka\Acl;
use Zend\Authentication\AuthenticationService;
use Zend\Permissions\Acl\AclInterface;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase as ZendAbstractHttpControllerTestCase;

use Omeka\Entity\Site;
use Omeka\Entity\SitePage;
use Omeka\Settings;
use Zend\Http\Request as HttpRequest;
abstract class AbstractHttpControllerTestCase extends ZendAbstractHttpControllerTestCase
{

public function setUp() {

parent::setUp();

}

public function postDispatch($url, $data) {
return $this->dispatch($url, HttpRequest::METHOD_POST,$data);
}

public function connectAdminUser() {
$entityManager = $this->getApplicationServiceLocator()->get('Omeka\EntityManager');
$user = $entityManager->find('Omeka\Entity\User',1);

$user->setIsActive(true);
$user->setRole('global_admin');
$user->setName('Tester');
$user->setEmail('admin@example.com');
$aclMock = $this->getMock('Omeka\Permissions\Acl');
$aclMock->expects($this->any())
->method('userIsAllowed')
->will($this->returnValue(true));

$authMock = $this->getMock('Zend\Authentication\AuthenticationService');
$authMock->expects($this->any())
->method('hasIdentity')
->will($this->returnValue(true));

$authMock->expects($this->any())
->method('getIdentity')
->will($this->returnValue($user));

$this->getApplicationServiceLocator()->setAllowOverride(true);


$this->getApplicationServiceLocator()->setService('Omeka\AuthenticationService', $authMock);
$this->getApplicationServiceLocator()->setService('Omeka\Acl', $aclMock);


}
public function getApplication()
{
// Return the application immediately if already set.
Expand All @@ -27,4 +75,65 @@ public function getApplication()

return $this->application;
}

protected function resetApplication()
{
$this->application = null;
}

protected function login($email, $password)
{
$serviceLocator = $this->getApplication()->getServiceManager();
$auth = $serviceLocator->get('Omeka\AuthenticationService');
$adapter = $auth->getAdapter();
$adapter->setIdentity($email);
$adapter->setCredential($password);
return $auth->authenticate();
}


protected function addSite($title) {
$site = new Site;
$site->setTitle($title);
$site->setSlug($title);
$site->setTheme('default');
$site->setNavigation([["type" =>"browse","data" => ["label"=>"Browse","query"=>""],"links"=>[]]]);
$site->setIsPublic(true);
$site->setItemPool(true);
$this->persistAndSave($site);
return $site;
}

public function persistAndSave($entity)
{

$em= $this->getApplicationServiceLocator()->get('Omeka\EntityManager');

$em->persist($entity);
$em->flush();
}


public function cleanTable($table_name) {
$this->getApplicationServiceLocator()->get('Omeka\Connection')->exec('DELETE FROM '.$table_name);
}


public function delete($entity)
{

$em= $this->getApplicationServiceLocator()->get('Omeka\EntityManager');
$entity=$em->find('Omeka\Entity\Site',1);
$em->remove($entity);
$em->flush();

}


public function setSettings($id,$value)
{
$settings = $this->getApplicationServiceLocator()->get('Omeka\Settings');
$settings->set($id,$value);
}

}
26 changes: 26 additions & 0 deletions application/src/View/Helper/SiteSelect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Omeka\View\Helper;
use Zend\Form\Element\Select;


/**
* A select menu containing all sites.
*/
class SiteSelect extends AbstractSelect
{

protected $emptyOption = 'All sites';

public function getValueOptions()
{
$sites = $this->getView()->api()->search('sites')->getContent();
$options = [];
foreach ($sites as $site) {
$options[$site->id()] = $site->title();
}

return $options;
}


}
2 changes: 1 addition & 1 deletion application/test/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
require '../../bootstrap.php';
require __DIR__ . '/../../bootstrap.php';

//make sure error reporting is on for testing
error_reporting(E_ALL);
Expand Down
2 changes: 1 addition & 1 deletion application/test/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="bootstrap.php" colors="true" backupGlobals="false">
<phpunit bootstrap="./bootstrap.php" colors="true" backupGlobals="false">
<testsuites>
<testsuite name="Omeka">
<directory>./OmekaTest</directory>
Expand Down

0 comments on commit 650862a

Please sign in to comment.