public function invoke(Piece_Unity_Context $context) { $user = new stdClass(); $context->setAttribute('user', $user); $validation = $context->getValidation(); $validation->getConfiguration()->addValidation('email', 'Email'); $validation->validate('Validation', $user); }
function testRemovingNullByteFromRequestParameters() { $_SERVER['REQUEST_METHOD'] = 'POST'; $_POST['foo'] = "foofoo"; $_POST['bar'] = array("bar1bar1", array("bar2bar2")); $config =& new Piece_Unity_Config(); $context =& Piece_Unity_Context::singleton(); $context->setConfiguration($config); $interceptor =& new Piece_Unity_Plugin_Interceptor_NullByteAttackPreventation(); $interceptor->invoke(); $request =& $context->getRequest(); $foo = $request->getParameter('foo'); $this->assertEquals(7, strlen($_POST['foo'])); $this->assertEquals(6, strlen($foo)); $this->assertEquals('foofoo', $foo); $bar = $request->getParameter('bar'); $this->assertEquals(9, strlen($_POST['bar'][0])); $this->assertEquals(9, strlen($_POST['bar'][1][0])); $this->assertEquals(8, strlen($bar[0])); $this->assertEquals(8, strlen($bar[1][0])); $this->assertEquals('bar1bar1', $bar[0]); $this->assertEquals('bar2bar2', $bar[1][0]); unset($_POST['foo']); unset($_POST['bar']); unset($_SERVER['REQUEST_METHOD']); }
/** * Creates a Piece_Unity_URI object based on the active flow execution or a given * flow ID. * * @param string $eventName * @param string $flowID * @return Piece_Unity_URI */ function &createURI($eventName, $flowID = null) { $context =& Piece_Unity_Context::singleton(); $continuation =& $context->getContinuation(); $uri =& new Piece_Unity_URI(is_null($flowID) ? $context->getScriptName() : $flowID); $uri->addQueryString($GLOBALS['PIECE_UNITY_Service_Continuation_FlowExecutionTicketKey'], is_null($flowID) ? $continuation->getActiveFlowExecutionTicket() : $continuation->getFlowExecutionTicketByFlowID($flowID)); $uri->addQueryString($context->getEventNameKey(), $eventName); return $uri; }
/** * Creates a Piece_Unity_URI object based on the active flow execution or a given * flow ID. * * @param string $eventName * @param string $flowID * @return Piece_Unity_URI */ public static function createURI($eventName, $flowID = null) { $context = Piece_Unity_Context::singleton(); $continuation = $context->getContinuation(); $uri = new Piece_Unity_URI(is_null($flowID) ? $context->getScriptName() : $flowID); $uri->setQueryVariable(self::$_flowExecutionTicketKey, is_null($flowID) ? $continuation->getActiveFlowExecutionTicket() : $continuation->getFlowExecutionTicketByFlowID($flowID)); $uri->setQueryVariable($context->getEventNameKey(), $eventName); return $uri; }
function testLoadingPlugins() { $context =& Piece_Unity_Context::singleton(); $context->setView("{$this->_target}LoadingPlugins"); $viewElement =& $context->getViewElement(); $viewElement->setElement('content', 'This is a dynamic content.'); $config =& $this->_getConfig(); $context->setConfiguration($config); $this->assertEquals('Hello World', trim($this->_render())); }
/** * Sets a single instance of Piece_Unity_Context class to a plugin, and * defines extension points and configuration points for the plugin. * And also the plug-in name is set. * * @param string $prefix */ function Piece_Unity_Plugin_Common($prefix = 'Piece_Unity_Plugin') { if (strlen($prefix)) { $this->_name = str_replace(strtolower("{$prefix}_"), '', strtolower(get_class($this))); } else { $this->_name = strtolower(get_class($this)); } $this->_context =& Piece_Unity_Context::singleton(); $this->_initialize(); $config =& $this->_context->getConfiguration(); $config->validateExtensionPoints($this->_name, array_keys($this->_extensionPoints)); if (Piece_Unity_Error::hasErrors()) { return; } $config->validateConfigurationPoints($this->_name, array_keys($this->_configurationPoints)); }
/** * Creates a Net_URL object with the given path, and replaces some pieces of * a URI when the URI is not external. * * @param string $path */ function initialize($path) { $context =& Piece_Unity_Context::singleton(); if (!$this->_isExternal && !preg_match('/^https?/', $path) && !$context->usingProxy()) { $path = $context->getAppRootPath() . $path; } $this->_url =& new Net_URL($path); }
function testConfigure() { $config =& new Piece_Unity_Config(); $config->setConfiguration('Configurator_PieceORM', 'configDirectory', $this->_cacheDirectory); $config->setConfiguration('Configurator_PieceORM', 'cacheDirectory', $this->_cacheDirectory); $config->setConfiguration('Configurator_PieceORM', 'mapperConfigDirectory', $this->_cacheDirectory); $context =& Piece_Unity_Context::singleton(); $context->setConfiguration($config); $configurator =& new Piece_Unity_Plugin_Configurator_PieceORM(); $configurator->invoke(); $yaml = Spyc::YAMLLoad("{$this->_cacheDirectory}/piece-orm-config.yaml"); $this->assertTrue(count($yaml)); $context =& Piece_ORM_Context::singleton(); $config =& $context->getConfiguration(); foreach ($yaml as $configuration) { $this->assertEquals($configuration['dsn'], $config->getDSN($configuration['name'])); $this->assertEquals($configuration['options'], $config->getOptions($configuration['name'])); } $this->assertEquals($this->_cacheDirectory, $GLOBALS['PIECE_ORM_Mapper_ConfigDirectory']); $this->assertEquals($this->_cacheDirectory, $GLOBALS['PIECE_ORM_Mapper_CacheDirectory']); $this->assertEquals($this->_cacheDirectory, $GLOBALS['PIECE_ORM_Metadata_CacheDirectory']); }
function testDetectCicularReferenceInObject() { $context =& Piece_Unity_Context::singleton(); $foo =& new stdClass(); $bar =& new stdClass(); $baz =& new stdClass(); $foo->bar =& $bar; $bar->baz =& $baz; $baz->foo =& $foo; $viewElement =& $context->getViewElement(); $viewElement->setElementByRef('foo', $foo); $config =& new Piece_Unity_Config(); $config->setConfiguration('Renderer_JSON', 'contentType', 'text/javascript'); $config->setConfiguration('Renderer_JSON', 'include', array()); $config->setConfiguration('Renderer_JSON', 'exclude', array()); $config->setConfiguration('Renderer_JSON', 'useJSONP', true); $config->setConfiguration('Renderer_JSON', 'callbackKey', 'callback'); $context =& Piece_Unity_Context::singleton(); $context->setConfiguration($config); $renderer =& Piece_Unity_Plugin_Factory::factory('Renderer_JSON'); Piece_Unity_Error::disableCallback(); $renderer->invoke(); Piece_Unity_Error::enableCallback(); $this->assertTrue(Piece_Unity_Error::hasErrors()); $error = Piece_Unity_Error::pop(); $this->assertEquals(PIECE_UNITY_ERROR_UNEXPECTED_VALUE, $error['code']); $this->assertEquals(strtolower('_visitObject'), strtolower($error['context']['function'])); }
/** * Sets the application wide Piece_Unity_ViewElement object and * the Piece_Unity_Context object to the properties. */ function Piece_Unity_Service_FlexyElement() { $context =& Piece_Unity_Context::singleton(); $this->_viewElement =& $context->getViewElement(); $this->_context =& $context; }
/** * Redirects to the callback URI for the given realm. * * @param string $realm * @since Method available since Release 1.1.0 */ function redirectToCallbackURI($realm = null) { $context =& Piece_Unity_Context::singleton(); $context->setView($this->_state->getCallbackURI($realm)); $config =& $context->getConfiguration(); $config->setConfiguration('Renderer_Redirection', 'isExternal', true); }
/** * @since Method available since Release 1.3.0 */ function testShouldInstantiateAControllerOnce() { $GLOBALS['PIECE_UNITY_Plugin_Renderer_FlexyTestCase_Controller_InstantiationCount'] = 0; $context =& Piece_Unity_Context::singleton(); $context->setView("{$this->_target}LayoutContent"); $config =& $this->_getConfig(); $config->setConfiguration("Renderer_{$this->_target}", 'useLayout', true); $config->setConfiguration("Renderer_{$this->_target}", 'layoutView', "{$this->_target}Layout"); $config->setConfiguration("Renderer_{$this->_target}", 'layoutDirectory', "{$this->_cacheDirectory}/templates/Layout"); $config->setConfiguration("Renderer_{$this->_target}", 'layoutCompileDirectory', "{$this->_cacheDirectory}/compiled-templates/Layout"); $config->setConfiguration('Renderer_Flexy', 'useController', true); $config->setConfiguration('Renderer_Flexy', 'controllerClass', 'Piece_Unity_Plugin_Renderer_FlexyTestCase_Controller'); $config->setConfiguration('Renderer_Flexy', 'controllerDirectory', "{$this->_cacheDirectory}/lib"); $context->setConfiguration($config); $this->_render(); $this->assertEquals(1, $GLOBALS['PIECE_UNITY_Plugin_Renderer_FlexyTestCase_Controller_InstantiationCount']); }
/** * Configures the application. * * First this method tries to load a configuration from a configuration file in * the given configration directory using Piece_Unity_Config_Factory::factory(). * This method creates a new object if the load failed. * Second this method merges the given configuretion into the loaded * configuration. * Finally this method sets the configuration to the current context. * * @param string $configDirectory * @param string $cacheDirectory * @param Piece_Unity_Config $dynamicConfig * @since Method available since Release 1.5.0 */ function configure($configDirectory = null, $cacheDirectory = null, $dynamicConfig = null) { $this->_config =& Piece_Unity_Config_Factory::factory($configDirectory, $cacheDirectory); if (is_a($dynamicConfig, 'Piece_Unity_Config')) { $this->_config->merge($dynamicConfig); } $context =& Piece_Unity_Context::singleton(); $context->setConfiguration($this->_config); }
/** * @test */ public function redirectTheRequestToTheCallbackUriOfTheGivenRealm() { Piece_Unity_Service_Authentication_State::singleton()->setCallbackURI('Foo', 'http://example.org/path/to/callback.php'); $config = new Piece_Unity_Config(); $context = Piece_Unity_Context::singleton(); $context->setConfiguration($config); $this->assertFalse($this->_authentication->isAuthenticated('Foo')); $this->_authentication->login('Foo'); $this->assertTrue($this->_authentication->hasCallbackURI('Foo')); $this->_authentication->redirectToCallbackURI('Foo'); $this->assertEquals('http://example.org/path/to/callback.php', $context->getView()); $this->assertTrue($config->getConfiguration('Renderer_Redirection', 'isExternal')); }
/** * Creates a Net_URL object with the given path, and replaces some pieces of * a URL when the URL is not external. * * @param string $path */ function initialize($path) { $this->_url =& new Net_URL($path); if ($this->_isExternal) { return; } $context =& Piece_Unity_Context::singleton(); if (!$this->_isRedirection && $context->usingProxy() && array_key_exists('HTTP_X_FORWARDED_SERVER', $_SERVER)) { if ($this->_url->host != $_SERVER['HTTP_X_FORWARDED_SERVER']) { $this->_url->host = $_SERVER['HTTP_X_FORWARDED_SERVER']; $this->_url->protocol = 'http'; } $this->_url->port = 80; } else { $this->_url->protocol = $_SERVER['SERVER_PORT'] != 443 ? 'http' : 'https'; $this->_url->host = $_SERVER['SERVER_NAME']; $this->_url->port = $_SERVER['SERVER_PORT']; $this->_url->path = $context->removeProxyPath($this->_url->path); } }
function testShouldAlwaysAuthenticateIfTheScriptNameIsListedInExcludes() { $_SERVER['REQUEST_URI'] = '/wiki/show.php'; $config =& new Piece_Unity_Config(); $config->setConfiguration('Authorization', 'permissions', array('^/wiki/.*' => 'WIKI_ADMIN')); $config->setConfiguration('Authorization', 'excludes', array('^/wiki/show\\.php')); $context =& Piece_Unity_Context::singleton(); $context->setConfiguration($config); $authorization =& Piece_Unity_Plugin_Factory::factory('Authorization'); $GLOBALS['PIECE_UNITY_Plugin_Interceptor_AuthorizationTestCase_hasPermission'] = true; $this->assertTrue($authorization->invoke()); $GLOBALS['PIECE_UNITY_Plugin_Interceptor_AuthorizationTestCase_hasPermission'] = false; $this->assertTrue($authorization->invoke()); }
/** * Sets a Piece_Right_Result object as a view element and a flow * attribute. * * @param string $validationSetName * @param Piece_Right_Results &$results * @static */ function setResultsAsViewElementAndFlowAttribute($validationSetName, &$results) { $context =& Piece_Unity_Context::singleton(); $viewElement =& $context->getViewElement(); $viewElement->setElementByRef(Piece_Unity_Validation::_createResultsName($validationSetName), $results); $continuation =& $context->getContinuation(); if (!is_null($continuation)) { $continuation->setAttributeByRef(Piece_Unity_Validation::_createResultsName($validationSetName), $results); } }
/** * @since Method available since Release 1.0.0 */ private function _configure($configurations) { $config = new Piece_Unity_Config(); foreach ($configurations as $key => $configuration) { $config->setConfiguration('Interceptor_Authentication', $key, $configuration); } $context = Piece_Unity_Context::singleton(); $context->setConfiguration($config); $context->setView('Foo'); $session = $context->getSession(); @$session->start(); }
function testFilterPrefixes() { $oldFilterPrefixes = $GLOBALS['PIECE_RIGHT_Filter_Prefixes']; $oldFilterDirectories = $GLOBALS['PIECE_RIGHT_Filter_Directories']; $config =& new Piece_Unity_Config(); $config->setConfiguration('KernelConfigurator', 'validationFilterPrefixes', array('KernelConfiguratorTestCaseAlias')); $config->setConfiguration('KernelConfigurator', 'validationFilterDirectories', array(dirname(__FILE__) . '/KernelConfiguratorTestCase')); $context =& Piece_Unity_Context::singleton(); $context->setConfiguration($config); $configurator =& new Piece_Unity_Plugin_KernelConfigurator(); $configurator->invoke(); $foo =& Piece_Right_Filter_Factory::factory('FooFilter'); $this->assertTrue(is_object($foo)); $this->assertTrue(is_a($foo, 'KernelConfiguratorTestCaseAlias_FooFilter')); Piece_Right_Filter_Factory::clearInstances(); $GLOBALS['PIECE_RIGHT_Filter_Directories'] = $oldFilterDirectories; $GLOBALS['PIECE_RIGHT_Filter_Prefixes'] = $oldFilterPrefixes; }
/** * Gets a flow ID. * * @return string * @static */ function getFlowID() { if (!is_null($GLOBALS['PIECE_UNITY_Continuation_FlowID'])) { return $GLOBALS['PIECE_UNITY_Continuation_FlowID']; } $context =& Piece_Unity_Context::singleton(); $request =& $context->getRequest(); return $request->hasParameter($GLOBALS['PIECE_UNITY_Continuation_FlowIDKey']) ? $request->getParameter($GLOBALS['PIECE_UNITY_Continuation_FlowIDKey']) : null; }
/** * @since Method available since Release 1.1.0 */ function testFieldValuesShouldBeRestoredByValidationSetAndContainer() { $cacheDirectory = dirname(__FILE__) . '/' . basename(__FILE__, '.php'); $context =& Piece_Unity_Context::singleton(); $validation =& $context->getValidation(); $validation->setConfigDirectory($cacheDirectory); $validation->setCacheDirectory($cacheDirectory); $config =& $validation->getConfiguration(); $config->addValidation('bar', 'Length', array('min' => 1, 'max' => 255)); $container =& new stdClass(); $container->foo = 'bar'; $container->bar = 'baz'; $flexyForm =& new Piece_Unity_Service_FlexyElement(); $flexyForm->restoreValues('FieldValues', $container); $viewElement =& $context->getViewElement(); $elements = $viewElement->getElement('_elements'); $this->assertEquals(2, count(array_keys($elements))); $this->assertTrue(array_key_exists('foo', $elements)); $this->assertEquals('bar', $elements['foo']['_value']); $this->assertTrue(array_key_exists('bar', $elements)); $this->assertEquals('baz', $elements['bar']['_value']); $cache =& new Cache_Lite_File(array('cacheDir' => "{$cacheDirectory}/", 'masterFile' => '', 'automaticSerialization' => true, 'errorHandlingAPIBreak' => true)); $cache->clean(); }
function _assertTurnOffLayoutByHTTPAccept($turnOffLayoutByHTTPAccept, $result) { $context =& Piece_Unity_Context::singleton(); $context->setView("{$this->_target}LayoutContent"); $viewElement =& $context->getViewElement(); $viewElement->setElement('foo', 'This is an element for the content.'); $viewElement->setElement('bar', 'This is an element for the layout.'); $config =& $this->_getConfig(); $config->setConfiguration("Renderer_{$this->_target}", 'turnOffLayoutByHTTPAccept', $turnOffLayoutByHTTPAccept); $config->setConfiguration("Renderer_{$this->_target}", 'useLayout', true); $config->setConfiguration("Renderer_{$this->_target}", 'layoutView', "{$this->_target}Layout"); $config->setConfiguration("Renderer_{$this->_target}", 'layoutDirectory', "{$this->_cacheDirectory}/templates/Layout"); $config->setConfiguration("Renderer_{$this->_target}", 'layoutCompileDirectory', "{$this->_cacheDirectory}/compiled-templates/Layout"); $context->setConfiguration($config); $_SERVER['HTTP_ACCEPT'] = 'application/x-piece-html-fragment'; $this->assertEquals($result, rtrim($this->_render())); unset($_SERVER['HTTP_ACCEPT']); }
function testAdjustingSessionCookiePathToOff() { $_SERVER['HTTP_X_FORWARDED_FOR'] = '1.2.3.4'; $previousSessionCookiePath = ini_get('session.cookie_path'); ini_set('session.cookie_path', '/bar'); $config =& new Piece_Unity_Config(); $config->setConfiguration('Interceptor_ProxyBasePath', 'adjustSessionCookiePath', false); $context =& Piece_Unity_Context::singleton(); $context->setProxyPath('/foo'); $context->setConfiguration($config); $interceptor =& new Piece_Unity_Plugin_Interceptor_ProxyBasePath(); $interceptor->invoke(); $this->assertEquals('/bar', ini_get('session.cookie_path')); ini_set('session.cookie_path', $previousSessionCookiePath); unset($_SERVER['HTTP_X_FORWARDED_FOR']); }