Skip to content

Commit

Permalink
do not add alternative languages if site uses only one language
Browse files Browse the repository at this point in the history
fixes #1
  • Loading branch information
cebe committed May 15, 2019
1 parent 48abf1c commit 289c4ab
Show file tree
Hide file tree
Showing 4 changed files with 416 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/controllers/SitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace cebe\luya\sitemap\controllers;

use luya\admin\models\Lang;
use luya\cms\models\Config;
use Yii;
use luya\cms\helpers\Url;
Expand Down Expand Up @@ -50,6 +51,8 @@ private function buildSitemapfile($sitemapFile)

$baseUrl = $host . Yii::$app->request->baseUrl;

$isSingleLanguageSite = Lang::find()->where(['is_deleted' => false])->count() == 1;

// create sitemap
$sitemap = new Sitemap($sitemapFile, true);

Expand Down Expand Up @@ -95,6 +98,11 @@ private function buildSitemapfile($sitemapFile)
}
$lastModified = $navItem->timestamp_update == 0 ? $navItem->timestamp_create : $navItem->timestamp_update;

// add single item with out language alternatives on single language site
if ($isSingleLanguageSite && count($urls) === 1) {
$urls = reset($urls);
}

$sitemap->addItem($urls, $lastModified);
}
}
Expand Down
105 changes: 105 additions & 0 deletions tests/MultiLangSitemapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace cebe\luya\sitemap\tests;

use cebe\luya\sitemap\tests\Setup;
use luya\cms\models\Config;
use cebe\luya\sitemap\Module;
use cebe\luya\sitemap\controllers\SitemapController;
use luya\testsuite\fixtures\ActiveRecordFixture;
use luya\cms\models\NavItem;
use luya\cms\models\Nav;
use luya\admin\models\Lang;
use yii\helpers\FileHelper;

class MultiLangSitemapTest extends Setup
{
public function getConfigArray()
{
return [
'id' => 'mytestapp',
'basePath' => dirname(__DIR__),
'aliases' => [
'runtime' => dirname(__DIR__) . '/tests/runtime',
],
'modules' => [
'cms' => 'luya\cms\frontend\Module',
],
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'sqlite::memory:',
],
'request' => [
'hostInfo' => 'https://luya.io',
],
'adminLanguage' => [
'class' => \luya\admin\components\AdminLanguage::class,
],
'composition' => [
'hidden' => false,
]
]
];
}

public function boolProvider()
{
return [
[true],
[false],
];
}

/**
* @dataProvider boolProvider
*/
public function testIgnoreHiddenModuleProperty($withHidden)
{
$module = new Module('sitemap');
$module->module = $this->app;
$module->withHidden = $withHidden;

SimpleSitemapTest::prepareBasicTableStructureAndData();

$ctrl = new SitemapController('sitemap', $module);
$response = $ctrl->actionIndex();
list($handle, $begin, $end) = $response->stream;

fseek($handle, $begin);
$content = stream_get_contents($handle);

$this->assertContainsTrimmed('<loc>https://luya.io</loc>', $content);
$this->assertContainsTrimmed('<loc>https://luya.io/en/foo</loc>', $content);

$this->assertContainsTrimmed('<loc>https://luya.io/en/foo-3</loc>', $content);
$this->assertContainsTrimmed('<loc>https://luya.io/en/foo-3/foo-4-child</loc>', $content);
$this->assertContainsTrimmed('<loc>https://luya.io/en/foo-3/foo-4-child/foo-5-child-child</loc>', $content);

// check correct language on nested pages
$this->assertContainsTrimmed('<loc>https://luya.io/de/foo-3-de</loc>', $content);
$this->assertContainsTrimmed('<loc>https://luya.io/de/foo-3-de/foo-4-child-de</loc>', $content);
$this->assertNotContains('<loc>https://luya.io/de/foo-3-de/foo-4-child</loc>', $content);
$this->assertNotContains('<loc>https://luya.io/de/foo-3/foo-4-child-de</loc>', $content);

if ($withHidden) {
// $module->withHidden = true; = 2 Pages in index
$this->assertContainsTrimmed('<loc>https://luya.io/en/foo-hidden</loc>', $content);
} else {
// $module->withHidden = false; = 1 Page in index
$this->assertNotContains('<loc>https://luya.io/en/foo-hidden</loc>', $content);
}

$this->assertNotContains('<loc>https://luya.io/not-to-show-404</loc>', $content);
$this->assertNotContains('<loc>https://luya.io/en/not-to-show-404</loc>', $content);
$this->assertNotContains('<loc>https://luya.io/de/not-to-show-404</loc>', $content);

$this->assertNotContains('<loc>https://luya.io/publish-check-past</loc>', $content);
$this->assertNotContains('<loc>https://luya.io/en/publish-check-past</loc>', $content);
$this->assertNotContains('<loc>https://luya.io/de/publish-check-past</loc>', $content);
$this->assertNotContains('<loc>https://luya.io/publish-check-future</loc>', $content);
$this->assertNotContains('<loc>https://luya.io/en/publish-check-future</loc>', $content);
$this->assertNotContains('<loc>https://luya.io/de/publish-check-future</loc>', $content);
$this->assertContainsTrimmed('<loc>https://luya.io/en/publish-check-present</loc>', $content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use luya\admin\models\Lang;
use yii\helpers\FileHelper;

class SitemapControllerTest extends Setup
class SimpleSitemapTest extends Setup
{
public function getConfigArray()
{
Expand Down Expand Up @@ -57,7 +57,7 @@ public function testIgnoreHiddenModuleProperty($withHidden)
$module->module = $this->app;
$module->withHidden = $withHidden;

$this->prepareBasicTableStructureAndData();
static::prepareBasicTableStructureAndData();

$ctrl = new SitemapController('sitemap', $module);
$response = $ctrl->actionIndex();
Expand Down Expand Up @@ -104,7 +104,7 @@ public function testEncodedUrls()
$this->assertSame('https://japan.com/jp/%E6%96%B0', $this->invokeMethod($ctrl, 'encodeUrl', ['https://japan.com/jp/新']));
}

private function prepareBasicTableStructureAndData()
public static function prepareBasicTableStructureAndData()
{
$navItemFixture = (new ActiveRecordFixture([
'modelClass' => NavItem::class,
Expand Down
Loading

0 comments on commit 289c4ab

Please sign in to comment.