From: Douglas C. <dc...@us...> - 2005-02-24 22:34:18
|
Update of /cvsroot/gallery/gallery2/modules/rewrite/test/phpunit In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15926/test/phpunit Added Files: AdminRewriteControllerTest.class RewriteHelperTest.class RewriteUrlGeneratorTest.class SetupRewriteControllerTest.class Log Message: URL rewriting module --- NEW FILE: AdminRewriteControllerTest.class --- <?php /* * $RCSfile: AdminRewriteControllerTest.class,v $ * * Gallery - a web based photo album viewer and editor * Copyright (C) 2000-2004 Bharat Mediratta * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /** * @version $Revision: 1.1 $ $Date: 2005/02/24 22:34:07 $ * @package Rewrite * @subpackage PHPUnit * @author Douglas Cau <do...@ca...> */ /** * AdminRewrite controller tests * * @package Rewrite * @subpackage PHPUnit * */ class AdminRewriteControllerTest extends GalleryControllerTestCase { function AdminRewriteControllerTest($methodName) { $this->GalleryControllerTestCase($methodName, 'rewrite:AdminRewrite'); } function setUp() { global $gallery; parent::setUp(); foreach (array('prefixMap') as $param) { list ($ret, $this->_saveParameters[$param]) = GalleryCoreApi::getPluginParameter('module', 'rewrite', $param); if ($ret->isError()) { print $ret->getAsHtml(); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } } } function tearDown() { global $gallery; parent::tearDown(); foreach ($this->_saveParameters as $key => $value) { $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', $key, $value); if ($ret->isError()) { print $ret->getAsHtml(); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } } } function testCancelRedirect() { GalleryUtilities::putRequestVariable('form[action][cancel]', 1); $results = $this->handleRequest(); $this->assertEquals(array('redirect' => array('view' => 'core:SiteAdmin', 'subView' => 'rewrite:AdminRewrite'), 'status' => array(), 'error' => array()), $results); } function testResetPrefixes() { GalleryUtilities::putRequestVariable('form[action][reset]', 1); $results = $this->handleRequest(); $this->assertEquals(array('redirect' => array('view' => 'core:SiteAdmin', 'subView' => 'rewrite:AdminRewrite'), 'status' => array('saved' => 1), 'error' => array()), $results); $results = RewriteHelper::getPrefixMap(); $this->assertEquals(array(), $results[1]); } function testSaveValidPrefixes() { GalleryUtilities::putRequestVariable('form[action][save]', 1); GalleryUtilities::putRequestVariable('form[prefix]', array( 'core:ShowItem' => 'test_show', 'core:DownloadItem' => 'test_download')); $results = $this->handleRequest(); $this->assertEquals(array('redirect' => array('view' => 'core:SiteAdmin', 'subView' => 'rewrite:AdminRewrite'), 'status' => array('saved' => 1), 'error' => array()), $results); $results = RewriteHelper::getPrefixMap(); $this->assertEquals(array('core:ShowItem' => 'test_show', 'core:DownloadItem' => 'test_download'), $results[1]); } function testSaveEmptyPrefixes() { GalleryUtilities::putRequestVariable('form[action][save]', 1); GalleryUtilities::putRequestVariable('form[prefix]', array( 'core:ShowItem' => '', 'core:DownloadItem' => '')); $results = $this->handleRequest(); $this->assertEquals(array('redirect' => array('view' => 'core:SiteAdmin', 'subView' => 'rewrite:AdminRewrite'), 'status' => array('saved' => 1), 'error' => array()), $results); $this->assertEquals(array(GalleryStatus::success(), array()), RewriteHelper::getPrefixMap()); } function testSaveBadPrefixes1() { GalleryUtilities::putRequestVariable('form[action][save]', 1); GalleryUtilities::putRequestVariable('form[prefix]', array('core:ShowItem' => 'modules')); $results = $this->handleRequest(); $this->assertEquals(array('delegate' => array('view' => 'core:SiteAdmin', 'subView' => 'rewrite:AdminRewrite'), 'status' => array(), 'error' => array('form[error][badPrefix]')), $results); } function testSaveBadPrefixes2() { GalleryUtilities::putRequestVariable('form[action][save]', 1); GalleryUtilities::putRequestVariable('form[prefix]', array('core:ShowItem' => 'test', 'core:DownloadItem' => 'test')); $results = $this->handleRequest(); $this->assertEquals(array('delegate' => array('view' => 'core:SiteAdmin', 'subView' => 'rewrite:AdminRewrite'), 'status' => array(), 'error' => array('form[error][badPrefix]')), $results); } } --- NEW FILE: RewriteHelperTest.class --- <?php /* * $RCSfile: RewriteHelperTest.class,v $ * * Gallery - a web based photo album viewer and editor * Copyright (C) 2000-2004 Bharat Mediratta * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /** * @version $Revision: 1.1 $ $Date: 2005/02/24 22:34:07 $ * @package Rewrite * @subpackage PHPUnit * @author Douglas Cau <do...@ca...> */ /** * Test RewriteHelper functionality * * @package Rewrite * @subpackage PHPUnit * */ class RewriteHelperTest extends GalleryTestCase { function RewriteHelperTest($methodName) { $this->GalleryTestCase($methodName); } function setUp() { global $gallery; parent::setUp(); $rewriteLocation = '/phpunit/var/www/localhost/htdocs/gallery2'; $rewriteBase = '/gallery2/'; foreach (array('galleryLocation' => 'http://example.com/gallery2/', 'usePrefixes' => 'true', 'prefixMap' => serialize(array())) as $param => $value) { list ($ret, $this->_saveParameters[$param]) = GalleryCoreApi::getPluginParameter('module', 'rewrite', $param); if ($ret->isError()) { print $ret->getAsHtml(); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', $param, $value); if ($ret->isError()) { print $ret->getAsHtml(); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } } $urlGenerator = new RewriteUrlGenerator(); $urlGenerator->init('phpunit_test.php'); $urlGenerator->registerViewPrefix('test', 'core:ShowItem'); $platform = new RewriteHelperTestPlatform(); $platform->init(); $gallery->setUrlGenerator($urlGenerator); $gallery->setPlatform($platform); } function tearDown() { global $gallery; parent::tearDown(); foreach ($this->_saveParameters as $key => $value) { $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', $key, $value); if ($ret->isError()) { print $ret->getAsHtml(); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } } } function testCheckFile() { global $gallery; $platform = $gallery->getPlatform(); $platform->init(); /* Make sure we pass if it's all good to write. */ list ($ret, $code) = RewriteHelper::checkFile(); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $this->assertEquals(REWRITE_STATUS_HTACCESS_READY, $code); /* Make sure we're ok if there's already a Gallery section. */ list ($ret, $expected) = RewriteHelper::getTemplate(); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $expected = explode("\n", $expected); foreach ($expected as $line => $string) { $expected[$line] .= "\n"; } $platform->setPreviousContent($expected); list ($ret, $code) = RewriteHelper::checkFile(); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $this->assertEquals(REWRITE_STATUS_OK, $code, 'existing gallery section'); } function testCheckFileNoPrefix() { global $gallery; $platform = $gallery->getPlatform(); $platform->init(); /* Dont use prefix urls */ $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', 'usePrefixes', 'false'); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } /* Make sure we pass if it's all good to write. */ list ($ret, $code) = RewriteHelper::checkFile(); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $this->assertEquals(REWRITE_STATUS_HTACCESS_READY, $code); } function testCheckBadFile() { global $gallery; $platform = $gallery->getPlatform(); $platform->init(); /* Make sure we fail if it's not writable. */ $platform->setIsWriteable(false); list ($ret, $code) = RewriteHelper::checkFile(); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $this->assertEquals(REWRITE_STATUS_HTACCESS_CANT_WRITE, $code, 'cant write'); /* Make sure we fail if it's not readable */ $platform->setIsReadable(false); list ($ret, $code) = RewriteHelper::checkFile(); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $this->assertEquals(REWRITE_STATUS_HTACCESS_CANT_READ, $code, 'cant read'); /* Make sure we fail if it's there's no file */ $platform->setFileExists(false); list ($ret, $code) = RewriteHelper::checkFile(); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $this->assertEquals(REWRITE_STATUS_HTACCESS_MISSING, $code, 'cant read'); } function testWriteBadFile() { global $gallery; $platform = $gallery->getPlatform(); $platform->init(); /* Make sure we fail if it's not writable. */ $platform->setIsWriteable(false); list ($ret, $code) = RewriteHelper::writeFile(); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $this->assertEquals(REWRITE_STATUS_HTACCESS_CANT_WRITE, $code, 'cant write'); /* Make sure we fail if it's not readable */ $platform->setIsReadable(false); list ($ret, $code) = RewriteHelper::writeFile(); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $this->assertEquals(REWRITE_STATUS_HTACCESS_CANT_READ, $code, 'cant read'); /* Make sure we fail if it's there's no file */ $platform->setFileExists(false); list ($ret, $code) = RewriteHelper::writeFile(); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $this->assertEquals(REWRITE_STATUS_HTACCESS_MISSING, $code, 'cant read'); $this->assertEquals(null, $platform->_fileContent, 'file content'); } function testGetPrefixMap() { $prefixMap = array( 'core:ShowItem' => 'test_show', 'core:DownloadItem' => 'test_download'); $ret = GalleryCoreApi::setPluginParameter( 'module', 'rewrite', 'prefixMap', serialize($prefixMap)); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $this->assertEquals(array(GalleryStatus::success(), $prefixMap), RewriteHelper::getPrefixMap()); } function testGetHtaccessPath() { $this->assertEquals(array(GalleryStatus::success(), dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/.htaccess'), RewriteHelper::getHtaccessPath()); } } /** * Test platform for this test * * @package Rewrite * @subpackage PHPUnit */ class RewriteHelperTestPlatform extends GalleryPlatform{ var $_isWriteable; var $_isReadable; var $_fileExists; var $_previousContent; var $_fileContent; function init() { $this->_isWriteable = true; $this->_isReadable = true; $this->_fileExists = true; $this->_previousContent = array(); } function setIsWriteable($value) { $this->_isWriteable = $value; } function setIsReadable($value) { $this->_isReadable = $value; } function setFileExists($value) { $this->_fileExists = $value; } function setPreviousContent($value) { $this->_previousContent = $value; } function file_exists($file) { if ($file == dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/.htaccess') { return $this->_fileExists; } return parent::file_exists($file); } function is_readable($file) { if ($file == dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/.htaccess') { return $this->_isReadable; } return parent::is_readable($file); } function is_writeable($file) { if ($file == dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/.htaccess') { return $this->_isWriteable; } return parent::is_writeable($file); } function file($file) { if ($file == dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/.htaccess') { return $this->_previousContent; } return parent::file($file); } function fopen($file, $mode, $useIncludePath=null, $zcontext=null) { if ($file == dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/.htaccess') { $this->_fileContent = ''; return '.htaccess'; } return parent::file($file, $mode, $useIncludePath, $zcontext); } function fwrite($handle, $string, $length=null) { if ($handle == '.htaccess') { $this->_fileContent .= $string; return strlen($string); } return fwrite($handle, $string, $length); } function fclose($handle) { if ($handle == '.htaccess') { return true; } return fclose($handle); } } --- NEW FILE: RewriteUrlGeneratorTest.class --- <?php /* * $RCSfile: RewriteUrlGeneratorTest.class,v $ * * Gallery - a web based photo album viewer and editor * Copyright (C) 2000-2004 Bharat Mediratta * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /** * @version $Revision: 1.1 $ $Date: 2005/02/24 22:34:07 $ * @package Rewrite * @subpackage PHPUnit * @author Douglas Cau <do...@ca...> */ /** * Test RewriteUrlGenerator functionality * * @package Rewrite * @subpackage PHPUnit * */ class RewriteUrlGeneratorTest extends GalleryTestCase { var $_urlGenerator; function RewriteUrlGeneratorTest($methodName) { $this->GalleryTestCase($methodName); } function setUp() { global $gallery; $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['SERVER_PORT'] = 80; foreach (array('galleryLocation' => 'http://example.com/gallery2/', 'usePrefixes' => 'true', 'prefixMap' => serialize(array())) as $param => $value) { list ($ret, $this->_saveParameters[$param]) = GalleryCoreApi::getPluginParameter('module', 'rewrite', $param); if ($ret->isError()) { print $ret->getAsHtml(); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', $param, $value); if ($ret->isError()) { print $ret->getAsHtml(); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } } list ($ret, $this->_saveCoreParameter) = GalleryCoreApi::getPluginParameter('module', 'core', 'misc.useShortUrls'); if ($ret->isError()) { print $ret->getAsHtml(); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $this->_urlGenerator = new RewriteUrlGenerator(); $this->_urlGenerator->init('phpunit_test.php'); $gallery->setUrlGenerator($this->_urlGenerator); parent::setUp(); } function tearDown() { global $gallery; parent::tearDown(); foreach ($this->_saveParameters as $key => $value) { $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', $key, $value); if ($ret->isError()) { print $ret->getAsHtml(); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } } $ret = GalleryCoreApi::setPluginParameter( 'module', 'core', 'misc.useShortUrls', $this->_saveCoreParameter); if ($ret->isError()) { print $ret->getAsHtml(); $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } } function testInit() { $this->assertEquals(false, $this->_urlGenerator->_error, 'error'); } function testRegisterViewPrefix() { $this->_urlGenerator->registerViewPrefix('test', 'core:ShowItem'); $this->assertEquals('test', $this->_urlGenerator->_viewPrefixRegistry['reverse']['core:ShowItem'], 'reverse'); $this->assertEquals('core:ShowItem', $this->_urlGenerator->_viewPrefixRegistry['forward']['test'], 'forward'); /* Make sure we dont write over the 'reverse' array */ $this->_urlGenerator->registerViewPrefix('test2', 'core:ShowItem'); $this->assertEquals('test', $this->_urlGenerator->_viewPrefixRegistry['reverse']['core:ShowItem'], 'reverse2'); $this->assertEquals('core:ShowItem', $this->_urlGenerator->_viewPrefixRegistry['forward']['test2'], 'forward2'); } function testGetCurrentUrl() { $expected = 'http://example.com/gallery2/phpunit_test.php'; $this->assertEquals($expected, $this->_urlGenerator->getCurrentUrl()); $this->assertEquals(false, $this->_urlGenerator->_error, 'error'); } function testGetCurrentUrlDir() { $expected = 'http://example.com/gallery2/'; $this->assertEquals($expected, $this->_urlGenerator->getCurrentUrlDir()); $this->assertEquals(false, $this->_urlGenerator->_error, 'error'); } function testGetCurrentShortUrl() { GalleryUtilities::putRequestVariable('RewriteUrlGenerator', '/test/album/picture.jpg.html'); $urlGenerator = new RewriteUrlGenerator(); $urlGenerator->init('phpunit_test.php'); $this->assertEquals(false, $urlGenerator->_error, 'init error'); $this->assertEquals('http://example.com/gallery2/phpunit_test.php/test/album/picture.jpg.html', $urlGenerator->getCurrentUrl(), 'get current url'); $this->assertEquals(false, $urlGenerator->_error, 'call error'); $this->assertEquals('http://example.com/gallery2/phpunit_test.php/test/album/picture.jpg.html', $urlGenerator->_currentUrl, 'get cached url'); } function testShortUrls() { global $gallery; $session =& $gallery->getSession(); /* Setup.. */ list ($ret, $testAlbum) = $this->_createRandomAlbum($this->_getRootId()); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $this->_markForCleanup($testAlbum); list ($ret, $testItem) = $this->_createRandomDataItem($testAlbum->getId()); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'misc.useShortUrls', 'false'); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } /* Test url.. */ $params = array('view' => 'core:ShowItem', 'itemId' => $testAlbum->getId()); foreach ($params as $key => $value) { $values[] = GalleryUtilities::prefixFormVariable($key) . '=' . htmlentities($value); } $short = 'http://example.com/gallery2/test/' . $testAlbum->getPathComponent() . '/'; if (!$session->isUsingCookies()) { $sid = GalleryUtilities::prefixFormVariable($session->getKey()) . '=' . $session->getId(); $values[] = $sid; $short .= '?' . $sid; } $itemParams = array('view' => 'core:ShowItem', 'itemId' => $testItem->getId()); foreach ($itemParams as $key => $value) { $itemValues[] = GalleryUtilities::prefixFormVariable($key) . '=' . htmlentities($value); } $itemShort = 'http://example.com/gallery2/test/' . $testAlbum->getPathComponent() . '/' . $testItem->getPathComponent() . '.html'; if (!$session->isUsingCookies()) { $sid = GalleryUtilities::prefixFormVariable($session->getKey()) . '=' . $session->getId(); $itemValues[] = $sid; $itemShort .= '?' . $sid; } /* View prefix for short urls.. */ if (isset($this->_urlGenerator->_viewPrefixRegistry['reverse']['core:ShowItem'])) { unset($this->_urlGenerator->_viewPrefixRegistry['reverse']['core:ShowItem']); } $this->_urlGenerator->registerViewPrefix('test', 'core:ShowItem'); /* Short url */ unset($params['forceShortUrls']); unset($itemParams['forceShortUrls']); $this->assertEquals($short, $this->_urlGenerator->generateUrl($params), 'short url, album'); $this->assertEquals($itemShort, $this->_urlGenerator->generateUrl($itemParams), 'short url, item'); /* Dont use prefix urls */ $this->_urlGenerator->_cache = array(); $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', 'usePrefixes', 'false'); if ($ret->isError()) { return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } /* Rebuild url's */ $params = array('view' => 'core:ShowItem', 'itemId' => $testAlbum->getId()); foreach ($params as $key => $value) { $values[] = GalleryUtilities::prefixFormVariable($key) . '=' . htmlentities($value); } $short = 'http://example.com/gallery2/' . $testAlbum->getPathComponent() . '/'; if (!$session->isUsingCookies()) { $sid = GalleryUtilities::prefixFormVariable($session->getKey()) . '=' . $session->getId(); $values[] = $sid; $short .= '?' . $sid; } $itemParams = array('view' => 'core:ShowItem', 'itemId' => $testItem->getId()); foreach ($itemParams as $key => $value) { $itemValues[] = GalleryUtilities::prefixFormVariable($key) . '=' . htmlentities($value); } $itemShort = 'http://example.com/gallery2/' . $testAlbum->getPathComponent() . '/' . $testItem->getPathComponent() . '.html'; if (!$session->isUsingCookies()) { $sid = GalleryUtilities::prefixFormVariable($session->getKey()) . '=' . $session->getId(); $itemValues[] = $sid; $itemShort .= '?' . $sid; } $this->assertEquals($short, $this->_urlGenerator->generateUrl($params), 'short url, album, no prefix'); $this->assertEquals($itemShort, $this->_urlGenerator->generateUrl($itemParams), 'short url, item, no prefix'); $this->assertEquals(false, $this->_urlGenerator->_error, 'call error'); } function testGetNavigationReturnUrl() { GalleryUtilities::putRequestVariable('foo', 'bar'); GalleryUtilities::putRequestVariable('return', 'http://blabla'); GalleryUtilities::putRequestVariable('returnName', 'name'); GalleryUtilities::putRequestVariable('navId', 'navId'); GalleryUtilities::putRequestVariable('fromNavId', 'fromNavId'); GalleryUtilities::putRequestVariable('RewriteUrlGenerator', '/album/image.jpg.html'); $expected = 'http://example.com/gallery2/phpunit_test.php?g2_foo=bar'; $this->assertEquals($expected, $this->_urlGenerator->getNavigationReturnUrl()); $this->assertEquals(false, $this->_urlGenerator->_error, 'call error'); } } --- NEW FILE: SetupRewriteControllerTest.class --- <?php /* * $RCSfile: SetupRewriteControllerTest.class,v $ * * Gallery - a web based photo album viewer and editor * Copyright (C) 2000-2004 Bharat Mediratta * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /** * @version $Revision: 1.1 $ $Date: 2005/02/24 22:34:07 $ * @package Rewrite * @subpackage PHPUnit * @author Douglas Cau <do...@ca...> */ /** * SetupRewrite controller tests * * @package Rewrite * @subpackage PHPUnit * */ class SetupRewriteControllerTest extends GalleryControllerTestCase { function SetupRewriteControllerTest($methodName) { $this->GalleryControllerTestCase($methodName, 'rewrite:SetupRewrite'); } function setUp() { global $gallery; parent::setUp(); $urlGenerator = $gallery->getUrlGenerator(); foreach (array('galleryLocation' => '', 'usePrefixes' => 'true') as $param => $value) { list ($ret, $this->_saveParameters[$param]) = GalleryCoreApi::getPluginParameter('module', 'rewrite', $param); if ($ret->isError()) { print $ret->getAsHtml(); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', $param, $value); if ($ret->isError()) { print $ret->getAsHtml(); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } } $gallery->setPlatform(new SetupRewriteControllerTestPlatform()); $urlGenerator->registerViewPrefix('test', 'core:ShowItem'); } function tearDown() { global $gallery; parent::tearDown(); foreach ($this->_saveParameters as $key => $value) { $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', $key, $value); if ($ret->isError()) { print $ret->getAsHtml(); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } } } function testWriteFile() { global $gallery; $platform = $gallery->getPlatform(); GalleryUtilities::putRequestVariable('form[action][write]', 1); list ($ret, $expected) = RewriteHelper::getTemplate(); if ($ret->isError()) { print $ret->getAsHtml(); return $this->failWithStatus($ret->wrap(__FILE__, __LINE__)); } $results = $this->handleRequest(); $this->assertEquals(array('redirect' => array('view' => 'core:SiteAdmin', 'subView' => 'rewrite:SetupRewrite'), 'status' => array('written' => 1), 'error' => array()), $results); $this->assertEquals($expected, $platform->_fileContent, 'file content'); } function testWriteBadFile() { global $gallery; $platform = $gallery->getPlatform(); $platform->setIsWriteable(false); GalleryUtilities::putRequestVariable('form[action][write]', 1); $results = $this->handleRequest(); $this->assertEquals(array('delegate' => array('view' => 'core:SiteAdmin', 'subView' => 'rewrite:SetupRewrite'), 'status' => array(), 'error' => array('form[error][cantWrite]')), $results); $this->assertEquals(null, $platform->_fileContent, 'file content'); } function testTestRedirect() { GalleryUtilities::putRequestVariable('form[action][test]', 1); $results = $this->handleRequest(); $this->assertEquals(array('redirect' => array('view' => 'core:SiteAdmin', 'subView' => 'rewrite:SetupRewrite'), 'status' => array(), 'error' => array()), $results); } function testDoneRedirect() { GalleryUtilities::putRequestVariable('form[action][done]', 1); GalleryUtilities::putRequestVariable('form[galleryLocation]', 'http://example.com/gallery2/'); $results = $this->handleRequest(); $this->assertEquals(array('redirect' => array('view' => 'core:SiteAdmin', 'subView' => 'core:AdminModules'), 'status' => array(), 'error' => array()), $results); } function testSave() { GalleryUtilities::putRequestVariable('form[action][save]', 1); GalleryUtilities::putRequestVariable('form[mode]', 'true'); GalleryUtilities::putRequestVariable('form[galleryLocation]', 'http://example.com/gallery2/'); $results = $this->handleRequest(); $this->assertEquals(array('redirect' => array('view' => 'core:SiteAdmin', 'subView' => 'rewrite:SetupRewrite'), 'status' => array('saved' => 1), 'error' => array()), $results); } } /** * Test platform for this test * * @package Rewrite * @subpackage PHPUnit */ class SetupRewriteControllerTestPlatform extends GalleryPlatform{ var $_isWriteable = true; var $_fileContent; function setIsWriteable($value) { $this->_isWriteable = $value; } function file_exists($file) { if ($file == dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/.htaccess') { return true; } return parent::file_exists($file); } function is_readable($file) { if ($file == dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/.htaccess') { return true; } return parent::is_readable($file); } function is_writeable($file) { if ($file == dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/.htaccess') { return $this->_isWriteable; } return parent::is_writeable($file); } function file($file) { if ($file == dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/.htaccess') { return array(); } return parent::file($file); } function fopen($file, $mode, $useIncludePath=null, $zcontext=null) { if ($file == dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/.htaccess') { $this->_fileContent = ''; return '.htaccess'; } return parent::file($file, $mode, $useIncludePath, $zcontext); } function fwrite($handle, $string, $length=null) { if ($handle == '.htaccess') { $this->_fileContent .= $string; return strlen($string); } return fwrite($handle, $string, $length); } function fclose($handle) { if ($handle == '.htaccess') { return true; } return fclose($handle); } } |