Skip to content

Commit

Permalink
Merge pull request #2 from gillesbourgeat/bump-version-1-1
Browse files Browse the repository at this point in the history
[WIP] Bump version 1 1
  • Loading branch information
gillesbourgeat committed Apr 11, 2016
2 parents 9115353 + 11b0e00 commit 558d450
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 31 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.1.0

- Adds the unit tests in the case of a single domain
- Adds the case there is a subfolder

# 1.0.1

- Fix ```installer-name``` in the composer.json file
Expand Down
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.0.3</version>
<version>1.1.0</version>
<authors>
<author>
<name>Gilles Bourgeat</name>
Expand Down
47 changes: 17 additions & 30 deletions EventListener/CanonicalUrlListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@

namespace CanonicalUrl\EventListener;

use Propel\Runtime\ActiveQuery\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Request;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Model\ConfigQuery;
use Thelia\Model\LangQuery;
use CanonicalUrl\Event\CanonicalUrlEvent;
use CanonicalUrl\Event\CanonicalUrlEvents;
use Thelia\Model\RewritingUrl;
use Thelia\Model\RewritingUrlQuery;

/**
* Class CanonicalUrlListener
Expand Down Expand Up @@ -51,46 +48,36 @@ public function __construct(Request $request)
*/
public function generateUrlCanonical(CanonicalUrlEvent $event)
{
$addUrlParameters = true;
if ($event->getUrl() !== null) {
return;
}

$parseUrlByCurrentLocale = $this->getParsedUrlByCurrentLocale();

// Be sure to use the proper domain name
$canonicalUrl = $parseUrlByCurrentLocale['scheme'] . '://' . $parseUrlByCurrentLocale['host'];

$uri = $this->request->getUri();

if (!empty($uri) && false !== $parse = parse_url($uri)) {
// Remove script name from path, preserving a potential subdirectory, e.g. http://somehost.com/mydir/index.php/...
$filePart = preg_replace("!/index(_dev)?\.php!", '', $parse['path']);

$canonicalUrl .= $filePart;

// If URL rewriting is enabled, check if our URL is rewritten.
// If it's the case, we will not add parameters to prevent duplicate content.
if (ConfigQuery::isRewritingEnable()) {
$pathList = [];
// preserving a potential subdirectory, e.g. http://somehost.com/mydir/index.php/...
$canonicalUrl .= $this->request->getBaseUrl();

$filePart = trim($filePart, '/');
// Remove script name from path, e.g. http://somehost.com/index.php/...
$canonicalUrl = preg_replace("!/index(_dev)?\.php!", '', $canonicalUrl);

while (! empty($filePart)) {
$pathList[] = $filePart;
$path = $this->request->getPathInfo();

$filePart = preg_replace("!^[^/]+/?!", '', $filePart);
}
if (!empty($path) && $path != "/") {
$canonicalUrl .= $path;

// Check if we have a rewriten URL
$addUrlParameters = 0 === RewritingUrlQuery::create()->filterByUrl($pathList, Criteria::IN)->count();
}
}

if ($addUrlParameters) {
$canonicalUrl = rtrim($canonicalUrl, '/');
/*if (!ConfigQuery::read('allow_slash_ended_uri', false)) {
$canonicalUrl = rtrim($canonicalUrl, '/');
}*/
} else {
$queryString = $this->request->getQueryString();

if (! empty($queryString)) {
$canonicalUrl .= '?' . $queryString;
$canonicalUrl .= '/?' . $queryString;
}

}

$event->setUrl($canonicalUrl);
Expand Down
228 changes: 228 additions & 0 deletions Tests/CanonicalUrlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
<?php
/*************************************************************************************/
/* This file is part of the module CanonicalUrl */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/

namespace CanonicalUrl\Tests;

use CanonicalUrl\Event\CanonicalUrlEvent;
use CanonicalUrl\EventListener\CanonicalUrlListener;
use Symfony\Component\HttpFoundation\Request;

/**
* Class CanonicalUrlTest
* @package CanonicalUrl\Tests
* @author Gilles Bourgeat <gilles.bourgeat@gmail.com>
*/
class CanonicalUrlTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
/*$config = $this->getMock('Thelia\Model\ConfigQuery');
$config->expects($this->any())
->method('read')
->with('allow_slash_ended_uri')
->will($this->returnValue(true));*/
}

public function testRemoveFileIndex()
{
$this->performList('http://myhost.com/test', [
'http://myhost.com/index.php/test',
'http://myhost.com/index.php/test/',
'http://myhost.com/index.php/test?page=22&list=1',
'http://myhost.com/index.php/test/?page=22&list=1'
]);
}

public function testRemoveFileIndexDev()
{
$this->performList('http://myhost.com/test', [
'http://myhost.com/index_dev.php/test',
'http://myhost.com/index_dev.php/test/',
'http://myhost.com/index_dev.php/test?page=22&list=1',
'http://myhost.com/index_dev.php/test/?page=22&list=1'
], $this->fakeServer(
'/var/www/web/index_dev.php',
'/index_dev.php'
));
}

public function testHTTPWithSubDomain()
{
$this->performList('http://mysubdomain.myhost.com/test', [
'http://mysubdomain.myhost.com/index.php/test/?page=22&list=1'
]);
}

public function testHTTPS()
{
$this->performList('https://myhost.com/test', [
'https://myhost.com/index.php/test/?page=22&list=1'
]);
}

public function testHTTPSWithSubDomain()
{
$this->performList('https://mysubdomain.myhost.com/test', [
'https://mysubdomain.myhost.com/index.php/test/?page=22&list=1'
]);
}

public function testHTTPWithSubdirectory()
{
$this->performList('http://myhost.com/web/test', [
'http://myhost.com/web/index.php/test',
'http://myhost.com/web/index.php/test/',
'http://myhost.com/web/index.php/test?page=22&list=1',
'http://myhost.com/web/index.php/test?page=22&list=1/'
], $this->fakeServer(
'/var/www/web/index.php',
'/web/index.php'
));

$this->performList('http://myhost.com/web/test', [
'http://myhost.com/web/index_dev.php/test',
'http://myhost.com/web/index_dev.php/test/',
'http://myhost.com/web/index_dev.php/test?page=22&list=1',
'http://myhost.com/web/index_dev.php/test?page=22&list=1/'
], $this->fakeServer(
'/var/www/web/index_dev.php',
'/web/index_dev.php'
));
}

public function testHTTPWithMultipleSubdirectory()
{
$this->performList('http://myhost.com/web/web2/web3/test', [
'http://myhost.com/web/web2/web3/index.php/test/?page=22&list=1'
], $this->fakeServer(
'/var/www/web/web2/web3/index.php',
'/web/web2/web3/index.php'
));

$this->performList('http://myhost.com/web/web2/web3/test', [
'http://myhost.com/web/web2/web3/index_dev.php/test/?page=22&list=1'
], $this->fakeServer(
'/var/www/web/web2/web3/index_dev.php',
'/web/web2/web3/index_dev.php'
));
}

public function testHTTPSWithSubdirectory()
{
$this->performList('https://myhost.com/web/test', [
'https://myhost.com/web/index.php/test/?page=22&list=1'
], $this->fakeServer(
'/var/www/web/index.php',
'/web/index.php'
));
}

public function testHTTPSWithMultipleSubdirectory()
{
$this->performList('https://myhost.com/web/web2/web3/test', [
'https://myhost.com/web/web2/web3/index.php/test/?page=22&list=1'
], $this->fakeServer(
'/var/www/web/web2/web3/index.php',
'/web/web2/web3/index.php'
));
}

public function testWithNoPath()
{
$this->performList('http://myhost.com/?list=22&page=1', [
'http://myhost.com?list=22&page=1',
'http://myhost.com/?list=22&page=1',
'http://myhost.com/index.php?list=22&page=1',
'http://myhost.com/index.php/?list=22&page=1'
]);

$this->performList('http://myhost.com/?list=22&page=1', [
'http://myhost.com/index_dev.php?list=22&page=1',
'http://myhost.com/index_dev.php/?list=22&page=1'
], $this->fakeServer(
'/var/www/web/index_dev.php',
'/index_dev.php'
));
}

public function testWithNoPathAndMultipleSubdirectory()
{
$this->performList('http://myhost.com/web/?list=22&page=1', [
'http://myhost.com/web/index.php?list=22&page=1',
'http://myhost.com/web/?list=22&page=1',
'http://myhost.com/web/index.php?list=22&page=1',
'http://myhost.com/web/index.php/?list=22&page=1'
], $this->fakeServer(
'/var/www/web/index.php',
'/web/index.php'
));

$this->performList('http://myhost.com/web/?list=22&page=1', [
'http://myhost.com/web/index_dev.php?list=22&page=1',
'http://myhost.com/web/index_dev.php/?list=22&page=1'
], $this->fakeServer(
'/var/www/web/index_dev.php',
'/web/index_dev.php'
));
}

public function testOverrideCanonicalEvent()
{
$canonicalUrlListener = new CanonicalUrlListener(Request::create('https://myhost.com/test'));

$event = new CanonicalUrlEvent();

// override canonical
$canonical = 'http://myscanonical.com';
$event->setUrl($canonical);

$canonicalUrlListener->generateUrlCanonical($event);

$this->assertEquals($canonical, $event->getUrl());
}

/**
* @param string $scriptFileName
* @param string $scriptName
* @return array
*/
protected function fakeServer(
$scriptFileName = '/var/www/web/index.php',
$scriptName = '/index.php'
) {
return [
'SCRIPT_FILENAME' => $scriptFileName,
'SCRIPT_NAME' => $scriptName
];
}

/**
* @param string $canonicalExpected canonical expected
* @param array $list array of uri
* @param array $server
*/
protected function performList($canonicalExpected, array $list, array $server = [])
{
if (empty($server)) {
$server = $this->fakeServer();
}

foreach ($list as $uri) {
$canonicalUrlListener = new CanonicalUrlListener(
Request::create($uri, 'GET', [], [], [], $server)
);

$event = new CanonicalUrlEvent();

$canonicalUrlListener->generateUrlCanonical($event);

$this->assertEquals($canonicalExpected, $event->getUrl());
}
}
}

0 comments on commit 558d450

Please sign in to comment.