/** * Decide if we have a test for this request * @param string request (optional will default to REQUEST_URI * @param boolean debug (default false), if true, will also return non active tests based on the request * @return mixed boolean false if no test, otherwise the ABTest will be returned */ public function findTestByUri($request = null, $debug = false) { if ($request === null) { $request = env('REQUEST_URI'); } $fields = array("{$this->SeoUri->alias}.uri", "{$this->alias}.slug", "{$this->alias}.id", "{$this->alias}.roll", "{$this->alias}.priority", "{$this->alias}.testable"); $conditions = array("AND" => array(array('OR' => array("{$this->alias}.start_date IS NULL", "{$this->alias}.start_date <=" => date('Y-m-d'))), array('OR' => array("{$this->alias}.end_date IS NULL", "{$this->alias}.end_date >=" => date('Y-m-d'))))); if (!$debug) { $conditions["{$this->alias}.is_active"] = true; } //Test one of one. if ($test = $this->find('first', array('conditions' => array_merge(array("{$this->SeoUri->alias}.uri" => $request, "{$this->SeoUri->alias}.is_approved" => true), $conditions), 'contain' => array("{$this->SeoUri->alias}.uri"), 'fields' => $fields, 'order' => "{$this->alias}.priority ASC"))) { return $test; } //Check Many to Many and Many to One $cacheEngine = SeoUtil::getConfig('cacheEngine'); if (!empty($cacheEngine)) { $cacheKey = 'seo_ab_tests'; $tests = Cache::read($cacheKey, $cacheEngine); } if (!isset($tests) || empty($tests)) { $tests = $this->find('all', array('conditions' => $conditions, 'contain' => array("{$this->SeoUri->alias}.uri"), 'fields' => $fields, 'order' => "{$this->alias}.priority ASC")); if (!empty($tests) && !empty($cacheEngine)) { Cache::write($cacheKey, $tests, $cacheEngine); } } foreach ($tests as $test) { if (SeoUtil::requestMatch($request, $test[$this->SeoUri->alias]['uri'])) { return $test; } } return false; }
/** * test requestMatch * * @return void */ public function testRequestMatchNoUri() { $this->assertFalse(SeoUtil::requestMatch('/invalid')); }
/** * Returns if the incomming request matches the seo_uri defined. * @param incomming request * @param uri * @return boolean */ function requestMatch($request, $uri) { return SeoUtil::requestMatch($request, $uri); }
/** * Given a request, see if the uri matches. * @param string request * @param mixed string uri or uri_id to look up * @return boolean if request matches the URI given */ public function requestMatch($request, $uri = null) { if (is_int($uri)) { $uri = $this->field('uri', array('SeoUri.id' => $uri)); } return SeoUtil::requestMatch($request, $uri); }