Skip to content

Commit

Permalink
SeoUrl tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Stone Lasley committed May 8, 2014
1 parent 09bd80f commit 12c5107
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 4 deletions.
5 changes: 2 additions & 3 deletions Model/SeoUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public function findRedirectByRequest($request) {
'redirect' => '/',
'shortest' => -1
);

//Run import if we have no urls to look at.
if ($this->find('count') == 0) {
if ($this->import() == 0) {
Expand Down Expand Up @@ -141,9 +140,9 @@ public function findRedirectByRequest($request) {
* @param $path
* @return string file path to source.
*/
private function __getPathToSiteMap($path) {
private function __getPathToSiteMap($path = null) {
if ($path === null) {
return WWW_ROOT . DS . 'site-map.xml';
return WWW_ROOT . 'site-map.xml';
} else {
return $path;
}
Expand Down
112 changes: 111 additions & 1 deletion Test/Case/Model/SeoUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,83 @@ public function setUp() {
$this->SeoUrl = ClassRegistry::init('SeoUrl');
}

/**
* test findRedirectByRequest method
*/
public function testFindRedirectByRequest() {
$this->SeoUrl->settings['active'] = true;
$this->SeoUrl->settings['cost_add'] = 1;
$this->SeoUrl->settings['cost_change'] = 1;
$this->SeoUrl->settings['cost_delete'] = 1;

$result = $this->SeoUrl->findRedirectByRequest("/products_url");

$this->assertEquals($result, array('redirect' => '/products', 'shortest' => 4));

$result = $this->SeoUrl->findRedirectByRequest("/invalid_categories");

$this->assertEquals($result, array('redirect' => '/categories', 'shortest' => 8));

$result = $this->SeoUrl->findRedirectByRequest("/recipes_with_some_other");

$this->assertEquals($result, array('redirect' => '/recipes', 'shortest' => 16));
}

/**
* test findRedirectByRequest method with higher cost
*/
public function testFindRedirectByRequestWithHigherCost() {
$this->SeoUrl->settings['active'] = true;
$this->SeoUrl->settings['cost_add'] = 1;
$this->SeoUrl->settings['cost_change'] = 2;
$this->SeoUrl->settings['cost_delete'] = 3;

$result = $this->SeoUrl->findRedirectByRequest("/");

$this->assertEquals(
array(
'redirect' => '/recipes',
'shortest' => 7
),
$result
);
}

/**
* test findRedirectByRequest method with inactive
*/
public function testFindRedirectByRequestWithInactive() {
$this->SeoUrl->settings['active'] = false;

$result = $this->SeoUrl->findRedirectByRequest("/");

$this->assertFalse($result);
}

/**
* test findRedirectByRequest method
*/
public function testFindRedirectByRequestWithNoMatches() {
$this->SeoUrl = $this->getMockForModel('SeoUrl', array('find', 'import'));
$this->SeoUrl->settings['active'] = true;
$this->SeoUrl->expects($this->once())
->method('find')
->will($this->returnValue(0));
$this->SeoUrl->expects($this->once())
->method('import')
->will($this->returnValue(0));

$result = $this->SeoUrl->findRedirectByRequest("/");

$this->assertEquals(
array(
'redirect' => '/',
'shortest' => -1
),
$result
);
}

public function testLevenshtien() {
$request = "/content/Hearing-loss/Treatment";
$add = 1;
Expand All @@ -47,20 +111,66 @@ public function testLevenshtien() {
*/
public function testMissingSitemapImport() {
$this->SeoUrl->settings['active'] = true;

$result = $this->SeoUrl->import('invalid-map.xml');

$this->assertEquals('269', $result);
}

/**
* Test sitemap import
* Test import method
*/
public function testImport() {
$testMap = dirname(__FILE__) . DS . 'site-map.xml';
$this->SeoUrl->settings['active'] = true;

$result = $this->SeoUrl->import($testMap);

$this->assertEquals('23', $result);
}

/**
* Test import method with verbose
*/
public function testImportWithVerbose() {
$testMap = dirname(__FILE__) . DS . 'site-map.xml';
$this->SeoUrl->settings['active'] = true;

$result = $this->SeoUrl->import($testMap, false, true);

$this->assertEquals('23', $result);
}

/**
* Test import method with verbose failed save
*/
public function testImportWithVerboseFailedSave() {
$this->SeoUrl = $this->getMockForModel('SeoUrl', array('create', 'save'));
$this->SeoUrl->settings['active'] = true;
$this->SeoUrl->expects($this->atLeastOnce())
->method('create')
->will($this->returnValue(true));
$this->SeoUrl->expects($this->atLeastOnce())
->method('save')
->will($this->returnValue(false));
$testMap = dirname(__FILE__) . DS . 'site-map.xml';

$result = $this->SeoUrl->import($testMap, false, true);
$this->assertEquals(0, $result);
}

/**
* Test getPathToSiteMap method
*/
public function testGetPathToSiteMap() {
$method = new ReflectionMethod('SeoUrl', '__getPathToSiteMap');
$method->setAccessible(true);

$result = $method->invoke($this->SeoUrl);

$this->assertStringEndsWith('webroot/site-map.xml', $result);
}

public function tearDown() {
parent::tearDown();
unset($this->SeoUrl);
Expand Down

0 comments on commit 12c5107

Please sign in to comment.