/**
  * Sends a redirect to the specified URL using the specified method.
  *
  * Valid $type values include:
  *    REDIRECT_REFRESH  Uses the header refresh method
  *    REDIRECT_META  Sends a a META HTTP-EQUIV="Refresh" tag to the output
  *    REDIRECT_HEADER  Uses the header location method
  *
  * REDIRECT_HEADER is the default.
  *
  * @param string $url The URL to redirect the client browser to.
  * @param array|boolean $options An array of options for the redirect OR
  * indicates if redirect attempts should be counted and limited to 3 (latter is deprecated
  * usage; use count_attempts in options array).
  * @param string $type The type of redirection to attempt (deprecated, use type in
  * options array).
  * @param string $responseCode The type of HTTP response code HEADER to send for the
  * redirect (deprecated, use responseCode in options array)
  * @return void|boolean
  */
 public function sendRedirect($url, $options = false, $type = '', $responseCode = '')
 {
     if (!is_array($options)) {
         $options = array('count_attempts' => (bool) $options);
     }
     $options = array_merge(array('count_attempts' => false, 'type' => $type, 'responseCode' => $responseCode), $options);
     $url = str_replace('&', '&', $url);
     if (empty($url)) {
         $this->modx->log(modX::LOG_LEVEL_ERROR, "Attempted to redirect to an empty URL.");
         return false;
     }
     if (!$this->modx->getRequest()) {
         $this->modx->log(modX::LOG_LEVEL_FATAL, "Could not load request class.");
     }
     if (isset($options['preserve_request']) && !empty($options['preserve_request'])) {
         $this->modx->request->preserveRequest('referrer.redirected');
     }
     if ($options['count_attempts']) {
         /* append the redirect count string to the url */
         $currentNumberOfRedirects = isset($_REQUEST['err']) ? $_REQUEST['err'] : 0;
         if ($currentNumberOfRedirects > 3) {
             $this->modx->log(modX::LOG_LEVEL_FATAL, 'Redirection attempt failed - please ensure the resource you\'re trying to redirect to exists. <p>Redirection URL: <i>' . $url . '</i></p>');
         } else {
             $currentNumberOfRedirects += 1;
             if (strpos($url, "?") > 0) {
                 $url .= "&err={$currentNumberOfRedirects}";
             } else {
                 $url .= "?err={$currentNumberOfRedirects}";
             }
         }
     }
     switch ($options['type']) {
         case 'REDIRECT_REFRESH':
             $header = 'Refresh: 0;URL=' . $url;
             break;
         case 'REDIRECT_META':
             $header = '<META HTTP-EQUIV="Refresh" CONTENT="0; URL=' . $url . '" />';
             echo $header;
             exit;
         default:
             if (strpos($url, '://') === false && !(substr($url, 0, 1) === '/' || substr($url, 0, 2) === './' || substr($url, 0, 3) === '../')) {
                 $url = $this->modx->getOption('site_url', null, '/') . $url;
             }
             $header = 'Location: ' . $url;
             break;
     }
     @session_write_close();
     if (!empty($options['responseCode']) && strpos($options['responseCode'], '30') !== false) {
         header($options['responseCode']);
     }
     header($header);
     exit;
 }
 /**
  * Function for logging changes of the order
  *
  * @param integer $order_id The id of the order
  * @param string $action The name of action made with order
  * @param string $entry The value of action
  *
  * @return boolean
  */
 public function orderLog($order_id, $action = 'status', $entry)
 {
     /* @var msOrder $order */
     if (!($order = $this->modx->getObject('msOrder', $order_id))) {
         return false;
     }
     if (empty($this->modx->request)) {
         $this->modx->getRequest();
     }
     $user_id = $action == 'status' && $entry == 1 || !$this->modx->user->id ? $order->get('user_id') : $this->modx->user->id;
     $log = $this->modx->newObject('msOrderLog', array('order_id' => $order_id, 'user_id' => $user_id, 'timestamp' => time(), 'action' => $action, 'entry' => $entry, 'ip' => $this->modx->request->getClientIp()));
     return $log->save();
 }
Beispiel #3
0
 /**
  * Grab a persistent instance of the xPDO class to share connection data
  * across multiple tests and test suites.
  * 
  * @param array $options An array of configuration parameters.
  * @return xPDO An xPDO object instance.
  */
 public static function _getConnection($options = array())
 {
     $modx = FiTestHarness::$modx;
     if (is_object($modx)) {
         if (!$modx->request) {
             $modx->getRequest();
         }
         if (!$modx->error) {
             $modx->request->loadErrorHandler();
         }
         $modx->error->reset();
         FiTestHarness::$modx = $modx;
         return FiTestHarness::$modx;
     }
     /* include config.core.php */
     $properties = array();
     $config = array();
     include strtr(realpath(dirname(__FILE__)) . '/config.inc.php', '\\', '/');
     require_once $config['modx_base_path'] . 'config.core.php';
     require_once MODX_CORE_PATH . 'config/' . MODX_CONFIG_KEY . '.inc.php';
     require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
     include_once strtr(realpath(dirname(__FILE__)) . '/properties.inc.php', '\\', '/');
     if (!defined('MODX_REQP')) {
         define('MODX_REQP', false);
     }
     $modx = new modX(null, $properties);
     $ctx = !empty($options['ctx']) ? $options['ctx'] : 'web';
     $modx->initialize($ctx);
     $debug = !empty($options['debug']);
     $modx->setDebug($debug);
     if (!empty($properties['logTarget'])) {
         $modx->setLogTarget($properties['logTarget']);
     }
     if (!empty($properties['logLevel'])) {
         $modx->setLogLevel($properties['logLevel']);
     }
     $modx->user = $modx->newObject('modUser');
     $modx->user->set('id', $modx->getOption('modx.test.user.id', null, 1));
     $modx->user->set('username', $modx->getOption('modx.test.user.username', null, 'test'));
     $modx->getRequest();
     $modx->getParser();
     $modx->request->loadErrorHandler();
     @error_reporting(E_ALL);
     @ini_set('display_errors', true);
     FiTestHarness::$modx = $modx;
     return $modx;
 }
Beispiel #4
0
 /**
  * Sets up the fixture, for example, opens a network connection.
  * This method is called before each test is executed.
  */
 protected function setUp()
 {
     // echo "\n---------------- SETUP --------------------";
     require_once dirname(__FILE__) . '/build.config.php';
     require_once dirname(__FILE__) . '/uthelpers.class.php';
     require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
     $this->utHelpers = new UtHelpers();
     $modx = new modX();
     $modx->initialize('mgr');
     $modx->getService('error', 'error.modError', '', '');
     $modx->getService('lexicon', 'modLexicon');
     $modx->getRequest();
     $homeId = $modx->getOption('site_start');
     $homeResource = $modx->getObject('modResource', $homeId);
     if ($homeResource instanceof modResource) {
         $modx->resource = $homeResource;
     } else {
         echo "\nNo Resource\n";
     }
     $modx->setLogLevel(modX::LOG_LEVEL_ERROR);
     $modx->setLogTarget('ECHO');
     require_once MODX_ASSETS_PATH . 'mycomponents/mycomponent/core/components/mycomponent/model/mycomponent/mycomponentproject.class.php';
     /* @var $categoryObj modCategory */
     $this->mc = new MyComponentProject($modx);
     $this->mc->init(array(), 'unittest');
     $this->modx =& $modx;
     $this->category = key($this->mc->props['categories']);
     $this->packageNameLower = $this->mc->packageNameLower;
     if ($this->category != 'UnitTest') {
         session_write_close();
         die('wrong config - NEVER run unit test on a real project!');
     }
     $category = $this->modx->getCollection('modCategory', array('category' => 'UnitTest'));
     foreach ($category as $categoryObj) {
         $categoryObj->remove();
     }
     $namespace = $this->modx->getObject('modNamespace', array('name' => 'unittest'));
     if ($namespace) {
         $namespace->remove();
     }
     $this->utHelpers->rrmdir($this->mc->targetRoot);
     $this->utHelpers->removeElements($this->modx, $this->mc);
     $this->utHelpers->removeResources($this->modx, $this->mc);
     //$this->mc->createCategory();
     //$this->mc->createNamespace();
 }
Beispiel #5
0
$modx= new modX('', $options);

/* set debugging/logging options */
//$modx->setDebug(E_ALL & ~E_NOTICE);
$modx->setLogLevel(modX::LOG_LEVEL_ERROR);
//$modx->setLogTarget('FILE');

/* initialize the proper context */
$ctx = isset($_REQUEST['ctx']) && !empty($_REQUEST['ctx']) ? $_REQUEST['ctx'] : 'mgr';
$modx->initialize($ctx);

if (defined('MODX_REQP') && MODX_REQP === false) {
} else if (!$modx->context->checkPolicy('load')) {
    @session_write_close();
    die();
}

if ($ctx == 'mgr') {
    $ml = $modx->getOption('manager_language',null,'en');
    if ($ml != 'en') {
        $modx->lexicon->load($ml.':core:default');
        $modx->setOption('cultureKey',$ml);
    }
}

/* handle the request */
$connectorRequestClass = $modx->getOption('modConnectorRequest.class',null,'modConnectorRequest');
$modx->config['modRequest.class'] = $connectorRequestClass;
$modx->getRequest();
$modx->request->sanitizeRequest();
 /**
  * Create or grab a reference to a static xPDO/modX instance.
  *
  * The instances can be reused by multiple tests and test suites.
  *
  * @param string $class A fixture class to get an instance of.
  * @param string $name A unique identifier for the fixture.
  * @param boolean $new
  * @param array $options An array of configuration options for the fixture.
  * @return object|null An instance of the specified fixture class or null on failure.
  */
 public static function &getFixture($class, $name, $new = false, array $options = array())
 {
     if (!$new && array_key_exists($name, self::$fixtures) && self::$fixtures[$name] instanceof $class) {
         $fixture =& self::$fixtures[$name];
     } else {
         $properties = array();
         include_once dirname(dirname(dirname(__FILE__))) . '/core/model/modx/modx.class.php';
         include dirname(__FILE__) . '/properties.inc.php';
         self::$properties = $properties;
         if (array_key_exists('debug', self::$properties)) {
             self::$debug = (bool) self::$properties['debug'];
         }
         $fixture = null;
         $driver = self::$properties['xpdo_driver'];
         switch ($class) {
             case 'modX':
                 if (!defined('MODX_REQP')) {
                     define('MODX_REQP', false);
                 }
                 if (!defined('MODX_CONFIG_KEY')) {
                     define('MODX_CONFIG_KEY', array_key_exists('config_key', self::$properties) ? self::$properties['config_key'] : 'test');
                 }
                 $fixture = new modX(null, self::$properties["{$driver}_array_options"]);
                 if ($fixture instanceof modX) {
                     $logLevel = array_key_exists('logLevel', self::$properties) ? self::$properties['logLevel'] : modX::LOG_LEVEL_WARN;
                     $logTarget = array_key_exists('logTarget', self::$properties) ? self::$properties['logTarget'] : (XPDO_CLI_MODE ? 'ECHO' : 'HTML');
                     $fixture->setLogLevel($logLevel);
                     $fixture->setLogTarget($logTarget);
                     if (!empty(self::$debug)) {
                         $fixture->setDebug(self::$properties['debug']);
                     }
                     $fixture->initialize(self::$properties['context']);
                     $fixture->user = $fixture->newObject('modUser');
                     $fixture->user->set('id', $fixture->getOption('modx.test.user.id', null, 1));
                     $fixture->user->set('username', $fixture->getOption('modx.test.user.username', null, 'test'));
                     $fixture->getRequest();
                     $fixture->getParser();
                     $fixture->request->loadErrorHandler();
                 }
                 break;
             case 'xPDO':
                 $fixture = new xPDO(self::$properties["{$driver}_string_dsn_test"], self::$properties["{$driver}_string_username"], self::$properties["{$driver}_string_password"], self::$properties["{$driver}_array_options"], self::$properties["{$driver}_array_driverOptions"]);
                 if ($fixture instanceof xPDO) {
                     $logLevel = array_key_exists('logLevel', self::$properties) ? self::$properties['logLevel'] : xPDO::LOG_LEVEL_WARN;
                     $logTarget = array_key_exists('logTarget', self::$properties) ? self::$properties['logTarget'] : (XPDO_CLI_MODE ? 'ECHO' : 'HTML');
                     $fixture->setLogLevel($logLevel);
                     $fixture->setLogTarget($logTarget);
                     if (!empty(self::$debug)) {
                         $fixture->setDebug(self::$properties['debug']);
                     }
                 }
                 break;
             default:
                 $fixture = new $class($options);
                 break;
         }
         if ($fixture !== null && $fixture instanceof $class) {
             self::$fixtures[$name] = $fixture;
         } else {
             die("Error setting fixture {$name} of expected class {$class}.");
         }
     }
     return $fixture;
 }
 protected function setUp()
 {
     require_once dirname(__FILE__) . '/build.config.php';
     require_once dirname(__FILE__) . '/uthelpers.class.php';
     require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
     $this->utHelpers = new UtHelpers();
     $modx = new modX();
     $this->modx =& $modx;
     $modx->initialize('mgr');
     $modx->getService('error', 'error.modError', '', '');
     $modx->getService('lexicon', 'modLexicon');
     $modx->getRequest();
     $homeId = $modx->getOption('site_start');
     $homeResource = $modx->getObject('modResource', $homeId);
     if ($homeResource instanceof modResource) {
         $modx->resource = $homeResource;
     } else {
         echo "\nNo Resource\n";
     }
     $modx->setLogLevel(modX::LOG_LEVEL_ERROR);
     $modx->setLogTarget('ECHO');
     require_once MODX_ASSETS_PATH . 'mycomponents/mycomponent/core/components/mycomponent/model/mycomponent/mycomponentproject.class.php';
     require_once MODX_ASSETS_PATH . 'mycomponents/mycomponent/core/components/mycomponent/model/mycomponent/lexiconcodefile.class.php';
     /* @var $categoryObj modCategory */
     $this->mc = new MyComponentProject($modx);
     $this->mc->init(array(), 'unittest');
     $this->dataDir = dirname(__FILE__) . '/data/';
     $this->dataDir = str_replace('\\', '/', $this->dataDir);
     $this->targetRoot = dirname(dirname(dirname(dirname(__FILE__)))) . '/unittest/';
     $this->targetRoot = str_replace('\\', '/', $this->targetRoot);
     $this->targetRoot = strtolower($this->targetRoot);
     $this->utHelpers->rrmdir($this->targetRoot);
     @mkdir($this->targetRoot, '0644', true);
     $this->targetCore = $this->targetRoot . 'core/components/unittest/';
     @mkdir($this->targetCore, '0644', true);
     $this->targetCore = str_replace('\\', '/', $this->targetCore);
     $this->targetLexDir = $this->targetCore . 'lexicon/';
     $this->targetLexDir = str_replace('\\', '/', $this->targetLexDir);
     @mkdir($this->targetLexDir . 'en', '0644', true);
     copy($this->dataDir . 'default.inc.php', $this->targetLexDir . 'en/default.inc.php');
     copy($this->dataDir . 'chunks.inc.php', $this->targetLexDir . 'en/chunks.inc.php');
     copy($this->dataDir . 'properties.inc.php', $this->targetLexDir . 'en/properties.inc.php');
     $this->targetModelDir = $this->targetCore . 'model/';
     $this->targetModelDir = str_replace('\\', '/', $this->targetModelDir);
     @mkdir($this->targetModelDir, '0644', true);
     copy($this->dataDir . 'example.class.php', $this->targetModelDir . 'example.class.php');
     $this->targetDataDir = $this->targetRoot . '_build/data/';
     $this->targetDataDir = str_replace('\\', '/', $this->targetDataDir);
     @mkdir($this->targetDataDir, '0644', true);
     copy($this->dataDir . 'transport.menus.php', $this->targetDataDir . 'transport.menus.php');
     copy($this->dataDir . 'transport.settings.php', $this->targetDataDir . 'transport.settings.php');
     $this->targetJsDir = $this->targetRoot . 'assets/components/unittest/js/';
     $this->targetJsDir = str_replace('\\', '/', $this->targetJsDir);
     @mkdir($this->targetJsDir, '0644', true);
     copy($this->dataDir . 'example.js', $this->targetJsDir . 'example.js');
     $this->targetChunkDir = $this->targetCore . 'elements/chunks/';
     $this->targetChunkDir = str_replace('\\', '/', $this->targetChunkDir);
     @mkdir($this->targetChunkDir, '0644', true);
     copy($this->dataDir . 'chunk1.chunk.html', $this->targetChunkDir . 'chunk1.chunk.html');
     $this->targetPropertiesDir = $this->targetRoot . '_build/data/properties/';
     $this->targetPropertiesDir = str_replace('\\', '/', $this->targetPropertiesDir);
     @mkdir($this->targetPropertiesDir, '0644', true);
     copy($this->dataDir . 'properties.propertyset1.propertyset.php', $this->targetPropertiesDir . 'properties.propertyset1.propertyset.php');
     copy($this->dataDir . 'properties.snippet1.snippet.php', $this->targetPropertiesDir . 'properties.snippet1.snippet.php');
     $this->languages = array('en' => array('default', 'properties', 'forms'));
     $this->assertNotEmpty($this->targetRoot, 'Empty Root');
     $this->assertNotEmpty($this->targetCore, 'Empty target core');
     $this->assertNotEmpty($this->targetLexDir, 'Empty target lex dir');
     $this->assertNotEmpty($this->targetModelDir, 'Empty Model dir');
     $this->assertNotEmpty($this->targetJsDir, 'Empty JS dir');
     $this->assertNotEmpty($this->targetChunkDir, 'Empty chunk dir');
 }