Skip to content

Commit

Permalink
Fix trailing slash used a product & category URL suffix
Browse files Browse the repository at this point in the history
Cover with unit tests
  • Loading branch information
ihor-sviziev committed Jun 27, 2017
1 parent 30c2c79 commit fe77c14
Showing 1 changed file with 231 additions and 3 deletions.
234 changes: 231 additions & 3 deletions app/code/Magento/UrlRewrite/Test/Unit/Model/Storage/DbStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

namespace Magento\UrlRewrite\Test\Unit\Model\Storage;

use \Magento\UrlRewrite\Model\Storage\DbStorage;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\UrlRewrite\Model\Storage\DbStorage;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;

class DbStorageTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -139,6 +137,8 @@ public function testFindOneByData()
->with($this->select)
->will($this->returnValue(['row1']));

$this->connectionMock->expects($this->never())->method('fetchAll');

$this->dataObjectHelper->expects($this->at(0))
->method('populateWithArray')
->with(['urlRewrite1'], ['row1'], \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
Expand All @@ -151,6 +151,234 @@ public function testFindOneByData()
$this->assertEquals(['urlRewrite1'], $this->storage->findOneByData($data));
}

public function testFindOneByDataWithRequestPath()
{
$origRequestPath = 'page-one';
$data = [
'col1' => 'val1',
'col2' => 'val2',
UrlRewrite::REQUEST_PATH => $origRequestPath,
];

$this->select->expects($this->at(1))
->method('where')
->with('col1 IN (?)', 'val1');

$this->select->expects($this->at(2))
->method('where')
->with('col2 IN (?)', 'val2');

$this->select->expects($this->at(3))
->method('where')
->with('request_path IN (?)', [$origRequestPath, $origRequestPath . '/']);

$this->connectionMock->expects($this->any())
->method('quoteIdentifier')
->will($this->returnArgument(0));

$this->connectionMock->expects($this->never())
->method('fetchRow');

$urlRewriteRowInDb = [
UrlRewrite::REQUEST_PATH => $origRequestPath,
UrlRewrite::REDIRECT_TYPE => 0,
];

$this->connectionMock->expects($this->once())
->method('fetchAll')
->with($this->select)
->will($this->returnValue([$urlRewriteRowInDb]));

$this->dataObjectHelper->expects($this->at(0))
->method('populateWithArray')
->with(['urlRewrite1'], $urlRewriteRowInDb, \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
->will($this->returnSelf());

$this->urlRewriteFactory->expects($this->at(0))
->method('create')
->will($this->returnValue(['urlRewrite1']));

$this->assertEquals(['urlRewrite1'], $this->storage->findOneByData($data));
}

public function testFindOneByDataWithRequestPathIsDifferent()
{
$origRequestPath = 'page-one';
$data = [
'col1' => 'val1',
'col2' => 'val2',
UrlRewrite::REQUEST_PATH => $origRequestPath,
];

$this->select->expects($this->at(1))
->method('where')
->with('col1 IN (?)', 'val1');

$this->select->expects($this->at(2))
->method('where')
->with('col2 IN (?)', 'val2');

$this->select->expects($this->at(3))
->method('where')
->with('request_path IN (?)', [$origRequestPath, $origRequestPath . '/']);

$this->connectionMock->expects($this->any())
->method('quoteIdentifier')
->will($this->returnArgument(0));

$this->connectionMock->expects($this->never())
->method('fetchRow');

$urlRewriteRowInDb = [
UrlRewrite::REQUEST_PATH => $origRequestPath . '/',
UrlRewrite::REDIRECT_TYPE => 0,
UrlRewrite::STORE_ID => 1,
];

$this->connectionMock->expects($this->once())
->method('fetchAll')
->with($this->select)
->will($this->returnValue([$urlRewriteRowInDb]));

$urlRewriteRedirect = [
'request_path' => $origRequestPath,
'redirect_type' => 301,
'store_id' => 1,
'entity_type' => 'custom',
'entity_id' => '0',
'target_path' => $origRequestPath . '/',
'description' => null,
'is_autogenerated' => '0',
'metadata' => null,
];

$this->dataObjectHelper->expects($this->at(0))
->method('populateWithArray')
->with(['urlRewrite1'], $urlRewriteRedirect, \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
->will($this->returnSelf());

$this->urlRewriteFactory->expects($this->at(0))
->method('create')
->will($this->returnValue(['urlRewrite1']));

$this->assertEquals(['urlRewrite1'], $this->storage->findOneByData($data));
}

public function testFindOneByDataWithRequestPathIsDifferent2()
{
$origRequestPath = 'page-one/';
$data = [
'col1' => 'val1',
'col2' => 'val2',
UrlRewrite::REQUEST_PATH => $origRequestPath,
];

$this->select->expects($this->at(1))
->method('where')
->with('col1 IN (?)', 'val1');

$this->select->expects($this->at(2))
->method('where')
->with('col2 IN (?)', 'val2');

$this->select->expects($this->at(3))
->method('where')
->with('request_path IN (?)', [rtrim($origRequestPath, '/'), rtrim($origRequestPath, '/') . '/']);

$this->connectionMock->expects($this->any())
->method('quoteIdentifier')
->will($this->returnArgument(0));

$this->connectionMock->expects($this->never())
->method('fetchRow');

$urlRewriteRowInDb = [
UrlRewrite::REQUEST_PATH => rtrim($origRequestPath, '/'),
UrlRewrite::REDIRECT_TYPE => 0,
UrlRewrite::STORE_ID => 1,
];

$this->connectionMock->expects($this->once())
->method('fetchAll')
->with($this->select)
->will($this->returnValue([$urlRewriteRowInDb]));

$urlRewriteRedirect = [
'request_path' => $origRequestPath,
'redirect_type' => 301,
'store_id' => 1,
'entity_type' => 'custom',
'entity_id' => '0',
'target_path' => rtrim($origRequestPath, '/'),
'description' => null,
'is_autogenerated' => '0',
'metadata' => null,
];

$this->dataObjectHelper->expects($this->at(0))
->method('populateWithArray')
->with(['urlRewrite1'], $urlRewriteRedirect, \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
->will($this->returnSelf());

$this->urlRewriteFactory->expects($this->at(0))
->method('create')
->will($this->returnValue(['urlRewrite1']));

$this->assertEquals(['urlRewrite1'], $this->storage->findOneByData($data));
}

public function testFindOneByDataWithRequestPathIsRedirect()
{
$origRequestPath = 'page-one';
$data = [
'col1' => 'val1',
'col2' => 'val2',
UrlRewrite::REQUEST_PATH => $origRequestPath,
];

$this->select->expects($this->at(1))
->method('where')
->with('col1 IN (?)', 'val1');

$this->select->expects($this->at(2))
->method('where')
->with('col2 IN (?)', 'val2');

$this->select->expects($this->at(3))
->method('where')
->with('request_path IN (?)', [$origRequestPath, $origRequestPath . '/']);

$this->connectionMock->expects($this->any())
->method('quoteIdentifier')
->will($this->returnArgument(0));

$this->connectionMock->expects($this->never())
->method('fetchRow');

$urlRewriteRowInDb = [
UrlRewrite::REQUEST_PATH => $origRequestPath . '/',
UrlRewrite::TARGET_PATH => 'page-A/',
UrlRewrite::REDIRECT_TYPE => 301,
UrlRewrite::STORE_ID => 1,
];

$this->connectionMock->expects($this->once())
->method('fetchAll')
->with($this->select)
->will($this->returnValue([$urlRewriteRowInDb]));

$this->dataObjectHelper->expects($this->at(0))
->method('populateWithArray')
->with(['urlRewrite1'], $urlRewriteRowInDb, \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
->will($this->returnSelf());

$this->urlRewriteFactory->expects($this->at(0))
->method('create')
->will($this->returnValue(['urlRewrite1']));

$this->assertEquals(['urlRewrite1'], $this->storage->findOneByData($data));
}

public function testReplace()
{
$urlFirst = $this->getMock(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class, [], [], '', false);
Expand Down

0 comments on commit fe77c14

Please sign in to comment.