Skip to content

Commit

Permalink
Merge branch 'fix_warning' into overall_test_BcUploader
Browse files Browse the repository at this point in the history
  • Loading branch information
dovanhung committed Nov 7, 2023
2 parents f26030b + fc14d96 commit a44b15e
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 259 deletions.
47 changes: 0 additions & 47 deletions plugins/baser-core/src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ public function beforeFilter(EventInterface $event)
$response = $this->redirectIfIsRequireMaintenance();
if ($response) return $response;

$this->__convertEncodingHttpInput();
$this->__cleanupQueryParams();

// インストーラー、アップデーターの場合はテーマを設定して終了
Expand Down Expand Up @@ -259,52 +258,6 @@ public function _blackHoleCallback($err, $exception)
throw new BadRequestException($message);
}

/**
* http経由で送信されたデータを変換する
* とりあえず、UTF-8で固定
*
* @return void
* @checked
* @noTodo
* @unitTest
*/
private function __convertEncodingHttpInput(): void
{
if ($this->getRequest()->getData()) {
$this->setRequest($this->request->withParsedBody($this->_autoConvertEncodingByArray($this->getRequest()->getData(), 'UTF-8')));
}
}

/**
* 配列の文字コードを変換する
*
* @param array $data 変換前データ
* @param string $outenc 変換後の文字コード
* @return array 変換後データ
* @checked
* @noTodo
* @unitTest
*/
protected function _autoConvertEncodingByArray($data, $outenc = 'UTF-8'): array
{
if (!$data) return [];
foreach($data as $key => $value) {
if (is_array($value)) {
$data[$key] = $this->_autoConvertEncodingByArray($value, $outenc);
continue;
}
$inenc = mb_detect_encoding((string)$value);
if (!$inenc) continue;
if (!in_array($inenc, Configure::read('BcEncode.detectOrder'))) continue;
if ($inenc === $outenc) continue;
// 半角カナは一旦全角に変換する
$value = mb_convert_kana($value, 'KV', $inenc);
$value = mb_convert_encoding($value, $outenc, $inenc);
$data[$key] = $value;
}
return $data;
}

/**
* クエリーパラメーターの調整
* 環境によって?キーにamp;が付加されてしまうため
Expand Down
25 changes: 14 additions & 11 deletions plugins/baser-core/src/View/Helper/BcBaserHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Cake\Core\Plugin;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\Http\ServerRequest;
use Cake\ORM\ResultSet;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;
Expand Down Expand Up @@ -2524,26 +2525,28 @@ public function afterRender(Event $event)
public function setCanonicalUrl()
{
$currentSite = $this->_View->getRequest()->getAttribute('currentSite');
if (!$currentSite) {
return;
}
if (!$currentSite) return;

$view = $this->getView();
$request = $view->getRequest();
if ($currentSite->device === 'smartphone') {
$sites = \Cake\ORM\TableRegistry::getTableLocator()->get('BaserCore.Sites');
$mainSite = $sites->getMainByUrl($this->_View->getRequest()->getPath());
$url = $mainSite->makeUrl(new CakeRequest($this->BcContents->getPureUrl(
$this->_View->getRequest()->getPath(),
$this->_View->getRequest()->getAttribute('currentSite')->id
)));
/** @var Site $mainSite */
$mainSite = $sites->getMainByUrl($request->getPath());
$url = $mainSite->makeUrl(new ServerRequest(['url' => $this->BcContents->getPureUrl(
$request->getPath(),
$request->getAttribute('currentSite')->id
)]));

} else {
$url = '/' . $this->_View->getRequest()->url;
$url = $request->getPath();
}
$url = preg_replace('/\.html$/', '', $url);
$url = preg_replace('/\/page:1$/', '', $url);
$url = preg_replace('/\\/index$/', '/', $url);
$this->_View->set('meta',
$view->assign('meta',
$this->BcHtml->meta('canonical',
$this->BcHtml->url($url, true),
$this->getUrl($url, true),
[
'rel' => 'canonical',
'type' => null,
Expand Down
36 changes: 32 additions & 4 deletions plugins/baser-core/tests/Factory/ContentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace BaserCore\Test\Factory;

use Cake\I18n\FrozenTime;
use CakephpFixtureFactories\Factory\BaseFactory as CakephpBaseFactory;
use Faker\Generator;

Expand Down Expand Up @@ -36,9 +37,36 @@ protected function setDefaultTemplate(): void
{
$this->setDefaultData(function (Generator $faker) {
return [
// set the model's default values
// For example:
// 'name' => $faker->lastName
'name' => $faker->text(50),
'plugin' => 'BaserCore',
'type' => 'Page',
'entity_id' => $faker->randomNumber(1, 100),
'url' => '',
'site_id' => 1,
'alias_id' => null,
'main_site_content_id' => null,
'parent_id' => null,
'lft' => null,
'rght' => null,
'level' => 1,
'title' => $faker->title(50),
'description' => $faker->text(100),
'eyecatch' => null,
'author_id' => 1,
'layout_template' => 'default',
'status' => true,
'publish_begin' => null,
'publish_end' => null,
'self_status' => true,
'self_publish_begin' => null,
'self_publish_end' => null,
'exclude_search' => false,
'created_date' => FrozenTime::now(),
'modified_date' => FrozenTime::now(),
'site_root' => false,
'deleted_date' => null,
'exclude_menu' => false,
'blank_link' => false,
];
});
}
Expand Down Expand Up @@ -71,7 +99,7 @@ public function treeNode($id, $siteId, $parentId, $name, $url, $entityId, $siteR
->setField('url', $url)
->setField('entity_id', $entityId)
->setField('status', true)
->setField('title', $this->getFaker()->title)
->setField('title', $this->getFaker()->title())
->setField('site_root', $siteRoot);
}

Expand Down
22 changes: 21 additions & 1 deletion plugins/baser-core/tests/Factory/SiteFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,22 @@ protected function setDefaultTemplate(): void
$this->setDefaultData(function (Generator $faker) {
return [
'name' => $faker->text(50),
'title' => $faker->text(50)
'title' => $faker->text(50),
'main_site_id' => null,
'display_name' => $faker->text(5),
'alias' => null,
'theme' => 'BcThemeSample',
'status' => true,
'keyword' => implode(',', [$faker->text(5), $faker->text(5), $faker->text(5)]),
'description' => $faker->text(100),
'use_subdomain' => false,
'relate_main_site' => false,
'device' => null,
'lang' => null,
'same_main_url' => false,
'auto_redirect' => false,
'auto_link' => false,
'domain_type' => null,
];
});
}
Expand All @@ -49,6 +64,7 @@ protected function setDefaultTemplate(): void
public function main(): SiteFactory
{
return $this->setField('id', 1)
->setField('name', '')
->setField('theme', 'BcFront')
->setField('status', true);
}
Expand All @@ -72,6 +88,7 @@ public function smartphone($id = null, $mainSiteId = 1): SiteFactory
->setField('auto_redirect', true)
->setField('auto_link', true)
->setField('theme', 'BcFront')
->setField('status', true)
->setField('domain_type', null);
}

Expand All @@ -93,6 +110,7 @@ public function english($id = null, $mainSiteId = 1): SiteFactory
->setField('same_main_url', false)
->setField('auto_redirect', true)
->setField('auto_link', false)
->setField('status', true)
->setField('domain_type', null);
}

Expand All @@ -114,6 +132,7 @@ public function anotherDomain($id = null, $mainSiteId = 1): SiteFactory
->setField('auto_redirect', false)
->setField('auto_link', false)
->setField('use_subdomain', true)
->setField('status', true)
->setField('domain_type', 2);
}

Expand All @@ -135,6 +154,7 @@ public function subDomain($id = null, $mainSiteId = 1): SiteFactory
->setField('auto_redirect', false)
->setField('auto_link', false)
->setField('use_subdomain', true)
->setField('status', true)
->setField('domain_type', 1);
}

Expand Down
3 changes: 2 additions & 1 deletion plugins/baser-core/tests/Factory/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ protected function setDefaultTemplate(): void
{
$this->setDefaultData(function (Generator $faker) {
return [
'name' => $faker->text()
'name' => $faker->text(),
'status' => true
];
});
}
Expand Down
29 changes: 0 additions & 29 deletions plugins/baser-core/tests/TestCase/Controller/AppControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,35 +223,6 @@ public function testRedirectIfIsRequireMaintenance()
Configure::write('debug', true);
}

/**
* test _autoConvertEncodingByArray
*/
public function test_autoConvertEncodingByArray()
{
$data = [
'test' => [
'test' => mb_convert_encoding('あいうえお', 'EUC-JP')
]
];
$result = $this->execPrivateMethod($this->AppController, '_autoConvertEncodingByArray', [$data, 'UTF-8']);
$this->assertEquals('あいうえお', $result['test']['test']);
}

/**
* test __convertEncodingHttpInput
*/
public function test__convertEncodingHttpInput()
{
$data = [
'test' => [
'test' => mb_convert_encoding('あいうえお', 'EUC-JP')
]
];
$this->AppController->setRequest($this->AppController->getRequest()->withParsedBody($data));
$this->execPrivateMethod($this->AppController, '__convertEncodingHttpInput');
$this->assertEquals('あいうえお', $this->AppController->getRequest()->getData('test.test'));
}

/**
* test __cleanupQueryParams
*/
Expand Down
57 changes: 40 additions & 17 deletions plugins/baser-core/tests/TestCase/View/Helper/BcBaserHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

namespace BaserCore\Test\TestCase\View\Helper;

use BaserCore\Test\Factory\ContentFactory;
use BaserCore\Test\Factory\SiteFactory;
use BaserCore\Test\Scenario\InitAppScenario;
use BaserCore\Test\Scenario\MultiSiteScenario;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;
use ReflectionClass;
use Cake\Event\Event;
use Cake\Core\Configure;
Expand All @@ -36,10 +41,16 @@
* @property BcBaserHelper $BcBaser
* @property FlashHelper $Flash
* @property UrlHelper $Url
* @property BcAdminAppView $BcAdminAppView
*/
class BcBaserHelperTest extends BcTestCase
{

/**
* Trait
*/
use ScenarioAwareTrait;

/**
* Fixtures
*
Expand Down Expand Up @@ -78,13 +89,6 @@ class BcBaserHelperTest extends BcTestCase
// 'baser.View.Helper.BcContentsHelper.ContentBcContentsHelper',
];

/**
* View
*
* @var View
*/
protected $_View;

/**
* __construct
* @param string $name
Expand Down Expand Up @@ -2058,22 +2062,41 @@ public function setAlternateUrlDataProvider()
* testSetAlternateUrl
* @dataProvider setCanonicalUrlDataProvider
*/
public function testSetCanonicalUrl($url, $expected)
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
Configure::write('BcSite.use_site_device_setting', true);
$this->BcBaser->request = $this->_getRequest($url);
public function testSetCanonicalUrl($siteId, $url, $expected)
{
// TODO FixtureManager が削除できたら、ここも削除する
// >>>
$this->truncateTable('sites');
$this->truncateTable('contents');
$this->truncateTable('content_folders');
$this->truncateTable('users');
$this->truncateTable('user_groups');
// <<<

$this->loadFixtureScenario(InitAppScenario::class);
SiteFactory::make([
'id' => 2,
'main_site_id' => 1,
'alias' => 's',
'device' => 'smartphone'
])->persist();
ContentFactory::make([
'site_id' => $siteId,
'url' => $url
])->persist();

$this->BcBaser->getView()->setRequest($this->getRequest($url));
$this->BcBaser->setCanonicalUrl();
$this->assertEquals($expected, $this->_View->fetch('meta'));
$this->assertEquals($expected, $this->BcBaser->getView()->fetch('meta'));
}

public function setCanonicalUrlDataProvider()
{
return [
['/', '<link href="http://localhost/" rel="canonical"/>'],
['/index.html', '<link href="http://localhost/" rel="canonical"/>'],
['/about/index.html', '<link href="http://localhost/about/" rel="canonical"/>'],
['/s/', '<link href="http://localhost/" rel="canonical"/>'],
[1, '/', '<link href="https://localhost/" rel="canonical"/>'],
[1, '/index.html', '<link href="https://localhost/" rel="canonical"/>'],
[1, '/about/index.html', '<link href="https://localhost/about/" rel="canonical"/>'],
[2, '/s/', '<link href="https://localhost/" rel="canonical"/>'],
];
}

Expand Down
Loading

0 comments on commit a44b15e

Please sign in to comment.