Skip to content

Commit

Permalink
Merge branch '5.1.x' into unittest_BcMigration_table
Browse files Browse the repository at this point in the history
  • Loading branch information
dovanhung committed Nov 26, 2024
2 parents 24792b3 + 4c345f1 commit d233658
Show file tree
Hide file tree
Showing 27 changed files with 394 additions and 38 deletions.
2 changes: 2 additions & 0 deletions plugins/baser-core/src/BcPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ public function routes(RouteBuilder $routes): void
* @return RouteBuilder
* @checked
* @noTodo
* @unitTest
*/
public function contentsRoutingForReverse(RouteBuilder $routes, string $plugin)
{
Expand Down Expand Up @@ -494,6 +495,7 @@ function(RouteBuilder $routes) {
* @return RouteBuilder
* @checked
* @noTodo
* @unitTest
*/
public function frontPageRouting(RouteBuilder $routes, string $plugin)
{
Expand Down
2 changes: 2 additions & 0 deletions plugins/baser-core/src/Event/BcEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class BcEventListener implements EventListenerInterface
* コンストラクタ
* @checked
* @noTodo
* @unitTest
*/
public function __construct()
{
Expand All @@ -78,6 +79,7 @@ public function __construct()
* @return array
* @checked
* @noTodo
* @unitTest
*/
public function implementedEvents(): array
{
Expand Down
2 changes: 2 additions & 0 deletions plugins/baser-core/src/Event/BcShortCodeEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function implementedEvents(): array
* @return void
* @checked
* @noTodo
* @unitTest
*/
public function afterRender(Event $event)
{
Expand All @@ -65,6 +66,7 @@ public function afterRender(Event $event)
* @return void
* @checked
* @noTodo
* @unitTest
*/
protected function _execShortCode(View $view)
{
Expand Down
10 changes: 7 additions & 3 deletions plugins/baser-core/src/Mailer/BcMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ public function __construct($config = null)
if ($site) $this->viewBuilder()
->setTheme($site->theme)
->setClassName('BaserCore.BcFrontEmail');
$this->setFrom([
BcSiteConfig::get('email') => BcSiteConfig::get('formal_name')
]);
$adminMail = BcSiteConfig::get('email');
if (strpos($adminMail, ',') !== false) {
[$fromAdmin] = explode(',', $adminMail);
} else {
$fromAdmin = $adminMail;
}
$this->setFrom($fromAdmin, BcSiteConfig::get('formal_name'));
}

/**
Expand Down
1 change: 1 addition & 0 deletions plugins/baser-core/src/Middleware/BcFrontMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class BcFrontMiddleware implements MiddlewareInterface
* @return ResponseInterface
* @checked
* @noTodo
* @unitTest
*/
public function process(
ServerRequestInterface $request,
Expand Down
8 changes: 6 additions & 2 deletions plugins/baser-core/src/Model/Table/SiteConfigsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ public function validationKeyValue(Validator $validator): Validator
{
$validator
->scalar('email')
->email('email', 255, __d('baser_core', '管理者メールアドレスの形式が不正です。'))
->notEmptyString('email', __d('baser_core', '管理者メールアドレスを入力してください。'));
->notEmptyString('email', __d('baser_core', '管理者メールアドレスを入力してください。'))
->add('email', ['emails' => [
'rule' => 'emails',
'provider' => 'bc',
'message' => __d('baser_core', '管理者メールアドレスの形式が不正です。')
]]);
$validator
->scalar('site_url')
->regex('site_url', '/^(http|https):/', __d('baser_core', 'WebサイトURLはURLの形式を入力してください。'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function getLayoutTemplates(EntityInterface $content)
if (in_array($parentTemplate, $templates)) {
unset($templates[$parentTemplate]);
}
$templates = array_merge($templates, ['' => __d('baser_core', '親フォルダの設定に従う') . '' . $parentTemplate . '']);
$templates = array_merge(['' => __d('baser_core', '親フォルダの設定に従う') . '' . $parentTemplate . ''], $templates);
}
return $templates;
}
Expand Down
9 changes: 3 additions & 6 deletions plugins/baser-core/src/Service/BcDatabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,8 @@ public function dropTable(string $tableName)
*/
public function loadDefaultDataPattern(string $theme, string $pattern, string $dbConfigKeyName = 'default'): bool
{
$plugins = array_merge(
['BaserCore'],
Configure::read('BcApp.corePlugins'),
BcUtil::getCurrentThemesPlugins()
);
$folderUtility = new BcFolder(Plugin::path($theme) . 'config' . DS . 'data' . DS . $pattern);
$plugins = $folderUtility->getFolders();

$db = $this->getDataSource($dbConfigKeyName);
$db->begin();
Expand Down Expand Up @@ -1052,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
38 changes: 38 additions & 0 deletions plugins/baser-core/tests/TestCase/BcPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,24 @@ public function test_applyAsTheme()
$this->assertEquals($updateTheme, $site->theme);
}

/**
* test frontPageRouting
*/
public function testFrontPageRouting()
{
// 管理画面のプラグイン用ルーティング
$result = Router::parseRequest($this->getRequest('/baser/admin/bc-blog/blog_contents/index'));
$this->assertEquals('BlogContents', $result['controller']);
$result = Router::parseRequest($this->getRequest('/baser/admin/bc-blog/blog_contents/edit/1'));
$this->assertEquals('BlogContents', $result['controller']);

// フロントエンドのプラグイン用ルーティング
$result = Router::parseRequest($this->getRequest('/bc-blog/blog_contents/index'));
$this->assertEquals('BlogContents', $result['controller']);
$result = Router::parseRequest($this->getRequest('/bc-blog/blog_contents/edit/1'));
$this->assertEquals('BlogContents', $result['controller']);
}

/**
* test Rest API
*/
Expand Down Expand Up @@ -646,4 +664,24 @@ public function testRestApi()
$this->assertEquals(0, count($result->pages));
}

/**
* test contentsRoutingForReverse
*/
public function testContentsRoutingForReverse()
{
SiteFactory::make(['id' => '1', 'main_site_id' => null])->persist();
SiteFactory::make(['id' => '2', 'main_site_id' => 1, 'alias' => 's'])->persist();
ContentFactory::make(['plugin' => 'BcBlog', 'type' => 'BlogContent', 'entity_id' => 31, 'url' => '/news/', 'site_id' => 1])->persist();
$this->BcPlugin = new BcPlugin(['name' => 'BcBlog']);
$routes = Router::createRouteBuilder('/');
$this->BcPlugin->routes($routes);
$this->getRequest('/');
$this->assertEquals('/news/', Router::url([
'plugin' => 'BcBlog',
'controller' => 'Blog',
'action' => 'index',
'entityId' => 31
]));
}

}
24 changes: 24 additions & 0 deletions plugins/baser-core/tests/TestCase/Event/BcEventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ public function tearDown(): void
parent::tearDown();
}

/**
* test __construct
*/
public function test__construct()
{
$this->BcEventListener = $this->getMockBuilder(BcEventListener::class)
->onlyMethods(['getAction'])
->setMockClassName('TestAction')
->getMock();

$this->assertEquals('TestAction', $this->BcEventListener->plugin);
}

/**
* Test getAction
*
Expand Down Expand Up @@ -80,4 +93,15 @@ public static function isActionDataProvider()
['Users.Index', 'Users.Index', true, true],
];
}


/**
* test implementedEvents
*/
public function testImplementedEvents()
{
$this->BcEventListener->events = ['isAction' => ['priority' => 100]];
$rs = $this->BcEventListener->implementedEvents();
$this->assertNotEmpty($rs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

use BaserCore\Event\BcShortCodeEventListener;
use BaserCore\TestSuite\BcTestCase;
use BaserCore\View\BcFrontAppView;
use Cake\Event\Event;

class BcShortCodeEventListenerTest extends BcTestCase
{
/**
* @var BcShortCodeEventListener
*/
protected $BcShortCodeEventListener;

public function setUp(): void
{
parent::setUp();
Expand All @@ -26,4 +33,34 @@ public function testImplementedEvents()
$this->assertTrue(is_array($this->BcShortCodeEventListener->implementedEvents()));
}

/**
* test afterRender
*/
public function testAfterRender()
{
//準備
$view = new BcFrontAppView($this->getRequest('/'));
$view->loadHelper('BcBaser', ['className' => 'BaserCore.BcBaser']);
$view->assign('content', '[BcBaser.getSitemap]');
$event = new Event('afterRender', $view);
//テストを実行
$this->BcShortCodeEventListener->afterRender($event);
//戻り値を確認
$this->assertTextContains('<ul class="menu ul-level-1">', $view->fetch('content'));
}

/**
* test _execShortCode
*/
public function test_execShortCode()
{
//準備
$view = new BcFrontAppView($this->getRequest('/'));
$view->loadHelper('BcBaser', ['className' => 'BaserCore.BcBaser']);
$view->assign('content', '[BcBaser.getSitemap]');
//テストを実行
$this->execPrivateMethod($this->BcShortCodeEventListener, '_execShortCode', [$view]);
//戻り値を確認
$this->assertTextContains('<ul class="menu ul-level-1">', $view->fetch('content'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,32 @@ public function test_setCurrent(): void
$this->assertNotEmpty($request->getAttribute('currentSite'));
}

/**
* Test process
*/
public function test_process(): void
{
ContentFactory::make([
'id' => 1,
'entity_id' => 1,
'url' => '/',
'site_id' => 1,
'status' => true,
])->persist();
SiteFactory::make([
'id' => 1,
'name' => '',
'title' => 'baserCMS inc.',
'status' => true,
])->persist();
$request = $this->getRequest()->withAttribute('isRequestView', true)->withQueryParams([
'Site' => SiteFactory::get(1),
'Content' => ContentFactory::get(1)
]);

$rs = $this->execPrivateMethod($this->BcFrontMiddleware, 'setCurrent', [$request]);
$this->assertNotEmpty($rs->getAttribute('currentContent'));
$this->assertNotEmpty($rs->getAttribute('currentSite'));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
class SiteConfigsServiceTest extends \BaserCore\TestSuite\BcTestCase
{

use EmailTrait;
/**
* Trait
*/
Expand Down Expand Up @@ -193,7 +194,7 @@ public function testSendTestMail()

//異常常テスト エラーになる
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The email set for `from` is empty.');
$this->SiteConfigs->sendTestMail([], '', 'メール送信テスト', 'メール送信テスト');
$this->expectExceptionMessage('Invalid email set for `from`. You passed `aaa`');
$this->SiteConfigs->sendTestMail(['email' => 'aaa'], '', 'メール送信テスト', 'メール送信テスト');
}
}
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
Loading

0 comments on commit d233658

Please sign in to comment.