Skip to content

Commit

Permalink
SeoABTests
Browse files Browse the repository at this point in the history
  • Loading branch information
Stone Lasley committed May 8, 2014
1 parent 12c5107 commit fd25b7f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 15 deletions.
31 changes: 20 additions & 11 deletions Model/SeoABTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,21 +272,28 @@ public function findTestByUri($request = null, $debug = false) {
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"
))) {
$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"
)
);

if (!empty($test)) {
return $test;
}

//Check Many to Many and Many to One
$cacheEngine = $this->getConfig('cacheEngine');

if (!empty($cacheEngine)) {
$cacheKey = 'seo_ab_tests';
$tests = Cache::read($cacheKey, $cacheEngine);
Expand All @@ -302,7 +309,9 @@ public function findTestByUri($request = null, $debug = false) {
Cache::write($cacheKey, $tests, $cacheEngine);
}
}

foreach ($tests as $test) {

if ($this->requestMatch($request, $test[$this->SeoUri->alias]['uri'])) {
return $test;
}
Expand Down
68 changes: 64 additions & 4 deletions Test/Case/Model/SeoABTestTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
App::uses('SeoABTest', 'Seo.Model');
App::uses('SeoTitle', 'Seo.Model');

/**
* SeoABTest Test Case
Expand Down Expand Up @@ -175,7 +176,7 @@ public function testIsTestable() {
'testable' => 'SeoTitle::callbackMethod'
)
);
$SeoTitle = $this->getMockForModel('SeoTitle', array('callbackMethod'));
$SeoTitle = $this->getMockForModel('Seo.SeoTitle', array('callbackMethod'));
$SeoTitle->expects($this->once())
->method('callbackMethod')
->will($this->returnValue(true));
Expand All @@ -193,7 +194,7 @@ public function testIsTestableWithNonTestable() {
$this->SeoABTest->alias => array (
)
);
$SeoTitle = $this->getMockForModel('SeoTitle', array('callbackMethod'));
$SeoTitle = $this->getMockForModel('Seo.SeoTitle', array('callbackMethod'));
$SeoTitle->expects($this->never())
->method('callbackMethod');
$this->assertTrue($this->SeoABTest->isTestable($data));
Expand Down Expand Up @@ -317,12 +318,71 @@ public function testTestableValidationWithEmpty() {
}

/**
* testFindTestByUri
* test findTestByUri method
*
* @return void
*/
public function testFindTestByUriWithNoRequest() {
$this->assertFalse($this->SeoABTest->findTestByUri());
$this->assertFalse($this->SeoABTest->findTestByUri(null, false));
}

/**
* test findTestByUri method
*
* @return void
*/
public function testFindTestByUri() {
$result = $this->SeoABTest->findTestByUri('/', false);

$this->assertTrue(isset($result[$this->SeoABTest->alias]));
$this->assertTrue(isset($result['SeoUri']));
$this->assertEquals('home', $result[$this->SeoABTest->alias]['slug']);
$this->assertEquals('/', $result['SeoUri']['uri']);
}

/**
* test findTestByUri method with cache
*
* @return void
*/
public function testFindTestByUriCached() {
Cache::clear();
$this->SeoABTest = $this->getMockForModel('SeoABTest', array('getConfig'));
$this->SeoABTest->expects($this->once())
->method('getConfig')
->will($this->returnValue(''));
// $this->SeoABTest->expects($this->at(0))
// ->method('find')
// ->will($this->returnValue(array()));
// $this->SeoABTest->expects($this->at(1))
// ->method('find')
// ->will($this->returnValue(array()));
$result = $this->SeoABTest->findTestByUri('/notfound', false);

$this->assertFalse($result);
}

/**
* test findTestByUri method with requestMatch
*
* @return void
*/
public function testFindTestByUriWithRequestMatch() {
Cache::clear();
$this->SeoABTest = $this->getMockForModel('SeoABTest', array('requestMatch', 'getConfig'));
$this->SeoABTest->expects($this->once())
->method('getConfig')
->will($this->returnValue('default'));
$this->SeoABTest->expects($this->once())
->method('requestMatch')
->will($this->returnValue(true));

$result = $this->SeoABTest->findTestByUri('/notfound', false);

$this->assertTrue(isset($result[$this->SeoABTest->alias]));
$this->assertTrue(isset($result['SeoUri']));
$this->assertEquals('home', $result[$this->SeoABTest->alias]['slug']);
$this->assertEquals('/', $result['SeoUri']['uri']);
}

/**
Expand Down

0 comments on commit fd25b7f

Please sign in to comment.