Skip to content

Commit

Permalink
Merge branch '5.1.x' into unitTest_BcPlugin_siteRouting
Browse files Browse the repository at this point in the history
  • Loading branch information
thangnn committed Nov 26, 2024
2 parents 2fe97aa + aab2513 commit 73bc25f
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class BcRedirectMainSiteMiddleware implements MiddlewareInterface
* @return ResponseInterface
* @checked
* @noTodo
* @unitTest
*/
public function process(
ServerRequestInterface $request,
Expand Down
2 changes: 1 addition & 1 deletion plugins/baser-core/src/Service/BcDatabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ public function writeSchema($table, $options)
'schema' => $schema
]);

$eventManager = EventManager::instance();
$eventManager = new EventManager();
$beforeRenderListeners = BcUtil::offEvent($eventManager, 'View.beforeRender');
$afterRenderListeners = BcUtil::offEvent($eventManager, 'View.afterRender');

Expand Down
15 changes: 15 additions & 0 deletions plugins/baser-core/src/Utility/BcUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace BaserCore\Utility;

use BaserCore\Error\BcException;
use BaserCore\Middleware\BcAdminMiddleware;
use BaserCore\Middleware\BcFrontMiddleware;
use BaserCore\Middleware\BcRequestFilterMiddleware;
Expand Down Expand Up @@ -1641,6 +1642,13 @@ public static function fgetcsvReg(&$handle, $length = null, $d = ',', $e = '"')
*/
public static function offEvent(EventManagerInterface $eventManager, string $eventKey)
{
$reflection = new ReflectionClass($eventManager);
$property = $reflection->getProperty('_isGlobal');
$property->setAccessible(true);
if($property->getValue($eventManager)) {
throw new BcException(__d('baser_core', 'グローバルイベントマネージャーからはイベントをオフにすることはできません。'));
}

$globalEventManager = $eventManager->instance();
$eventListeners = [
'local' => $eventManager->prioritisedListeners($eventKey),
Expand Down Expand Up @@ -1674,6 +1682,13 @@ public static function offEvent(EventManagerInterface $eventManager, string $eve
*/
public static function onEvent(EventManagerInterface $eventManager, string $eventKey, array $eventListeners)
{
$reflection = new ReflectionClass($eventManager);
$property = $reflection->getProperty('_isGlobal');
$property->setAccessible(true);
if($property->getValue($eventManager)) {
throw new BcException(__d('baser_core', 'グローバルイベントマネージャーからはイベントをオンにすることはできません。'));
}

$globalEventManager = $eventManager->instance();
if (!empty($eventListeners['local'])) {
foreach($eventListeners['local'] as $priority => $listeners) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* baserCMS : Based Website Development Project <https://basercms.net>
* Copyright (c) NPO baser foundation <https://baserfoundation.org/>
*
* @copyright Copyright (c) NPO baser foundation
* @link https://basercms.net baserCMS Project
* @since 5.0.0
* @license https://basercms.net/license/index.html MIT License
*/

namespace BaserCore\Test\TestCase\Middleware;

use BaserCore\Middleware\BcRedirectMainSiteMiddleware;
use BaserCore\Test\Factory\ContentFactory;
use BaserCore\Test\Factory\PageFactory;
use BaserCore\Test\Factory\SiteFactory;
use BaserCore\TestSuite\BcTestCase;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;

/**
* Class BcRedirectMainSiteMiddlewareTest
* @property BcRedirectMainSiteMiddleware $BcRedirectMainSiteMiddleware
*/
class BcRedirectMainSiteMiddlewareTest extends BcTestCase
{
/**
* Trait
*/
use ScenarioAwareTrait;

/**
* Set Up
*
* @return void
*/
public function setUp(): void
{
parent::setUp();
$this->BcRedirectMainSiteMiddleware = new BcRedirectMainSiteMiddleware();
}

/**
* Tear Down
*
* @return void
*/
public function tearDown(): void
{
unset($this->BcRedirectMainSiteMiddleware);
parent::tearDown();
}

/**
* test Process
* リダイレクトを確認
*/
public function test_process(): void
{
SiteFactory::make(['id' => 1, 'status' => true])->persist();
SiteFactory::make(['id' => 2, 'alias' => 's', 'auto_redirect' => true])->persist();
PageFactory::make(['id' => 1])->persist();
ContentFactory::make([
'url' => '/about',
'name' => 'about',
'plugin' => 'BaserCore',
'type' => 'Page',
'site_id' => 1,
'parent_id' => null,
'lft' => 1,
'rght' => 2,
'entity_id' => 1,
'site_root' => 2,
'status' => true
])->persist();

$request = $this->getRequest('/s/about')->withParam('plugin', 'BaserCore')->withParam('controller', 'Pages')->withParam('action', 'view');
$this->_response = $this->BcRedirectMainSiteMiddleware->process($request, $this->Application);
$this->assertResponseCode(302);

$request = $this->getRequest('/about')->withParam('plugin', 'BaserCore')->withParam('controller', 'Pages')->withParam('action', 'view');
$this->_response = $this->BcRedirectMainSiteMiddleware->process($request, $this->Application);
$this->assertResponseCode(200);
}
}
2 changes: 1 addition & 1 deletion plugins/baser-core/tests/TestCase/Utility/BcUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ public static function fgetcsvRegDataProvider()
*/
public function testOnEventOffEvent(): void
{
$eventManager = EventManager::instance();
$eventManager = new EventManager();
$eventKey = 'testOnEvent';
$bcEvenListener = new class extends BcEventListener {
public $events = ['event1'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public function edit(BlogContentsAdminServiceInterface $service, int $id)
* @return ResponseInterface
* @checked
* @noTodo
* @unitTest
*/
protected function redirectEditBlog(string $template): ResponseInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ public function test_redirectEditLayout()
*/
public function test_redirectEditBlog()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
$controller = new BlogContentsController($this->getRequest());
$rs = $this->execPrivateMethod($controller, 'redirectEditBlog', ['BcColumn']);
$this->assertEquals('https://localhost/baser/admin/bc-theme-file/theme_files/edit/BcFront/BcBlog/etc/Blog/BcColumn/index.php', $rs->getHeader('Location')[0]);
$this->assertEquals(302, $rs->getStatusCode());
}

}

0 comments on commit 73bc25f

Please sign in to comment.