Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/5.1.x' into 5.1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Nov 22, 2024
2 parents e22c89e + e364a88 commit 7541e39
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 23 deletions.
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
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
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
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 @@ -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'], '', 'メール送信テスト', 'メール送信テスト');
}
}
24 changes: 11 additions & 13 deletions plugins/bc-blog/src/Mailer/BlogCommentMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,17 @@ class BlogCommentMailer extends BcMailer
*/
public function sendCommentToAdmin(string $senderName, array $data)
{
$adminMail = BcSiteConfig::get('email');
if (strpos($adminMail, ',') !== false) {
[$fromAdmin] = explode(',', $adminMail);
} else {
$fromAdmin = $adminMail;
$adminMail = explode(',', BcSiteConfig::get('email'));
$fromAdmin = $adminMail[0];
foreach ($adminMail as $mailTo) {
$this->setTo($mailTo)
->setFrom($fromAdmin, $senderName)
->setReplyTo($fromAdmin)
->setSubject(__d('baser_core', '【{0}】コメントを受け付けました', $senderName))
->viewBuilder()
->setTemplate('BcBlog.blog_comment_admin')
->setVars($data);
}

$this->setTo($adminMail)
->setFrom($fromAdmin, $senderName)
->setReplyTo($fromAdmin)
->setSubject(__d('baser_core', '【{0}】コメントを受け付けました', $senderName))
->viewBuilder()
->setTemplate('BcBlog.blog_comment_admin')
->setVars($data);
}

/**
Expand All @@ -56,6 +53,7 @@ public function sendCommentToAdmin(string $senderName, array $data)
* @param array $data
* @checked
* @noTodo
* @unitTest
*/
public function sendCommentToUser(string $senderName, string $userMail, array $data)
{
Expand Down
13 changes: 12 additions & 1 deletion plugins/bc-blog/tests/TestCase/Mailer/BlogCommentMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class BlogCommentMailerTest extends BcTestCase
public function setUp(): void
{
parent::setUp();
SiteConfigFactory::make(['name' => 'email', 'value' => 'basertest@example.com'])->persist();
SiteConfigFactory::make(['name' => 'email', 'value' => 'basertest@example.com,basertest02@example.com'])->persist();
$this->BlogCommentMailer = new BlogCommentMailer();
}

Expand All @@ -26,7 +26,18 @@ public function tearDown(): void
public function testSendCommentToAdmin()
{
$this->BlogCommentMailer->sendCommentToAdmin('test', ['test' => 'test']);
$this->assertEquals(['basertest02@example.com' => 'basertest02@example.com'], $this->BlogCommentMailer->getTo());
$this->assertEquals('BcBlog.blog_comment_admin', $this->BlogCommentMailer->viewBuilder()->getTemplate());
$this->assertEquals(['test' => 'test'], $this->BlogCommentMailer->viewBuilder()->getVars());
}

/**
* test sendCommentToUser
*/
public function testSendCommentToUser()
{
$this->BlogCommentMailer->sendCommentToUser('admin baser', 'user@example.com', ['test' => 'test']);
$this->assertEquals('BcBlog.blog_comment_contributor', $this->BlogCommentMailer->viewBuilder()->getTemplate());
$this->assertEquals(['test' => 'test'], $this->BlogCommentMailer->viewBuilder()->getVars());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class BcCustomContentControllerEventListener extends \BaserCore\Event\BcControll
* Before render
* @checked
* @noTodo
* @unitTest
*/
public function startup(Event $event)
{
Expand All @@ -51,6 +52,7 @@ public function startup(Event $event)
* 管理画面メニュー用のデータをセットする
* @checked
* @noTodo
* @unitTest
*/
public function setAdminMenu()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ public function getEntryIndexTitle(CustomTable $table, CustomEntry $entry)
* プラグインのメタフィールドを表示する
* @checked
* @noTodo
* @unitTest
*/
public function displayPluginMeta()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?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 BcCustomContent\Test\TestCase\Event;

use BaserCore\Controller\Admin\UsersController;
use BaserCore\Service\BcDatabaseServiceInterface;
use BaserCore\Test\Factory\ContentFactory;
use BaserCore\Test\Scenario\InitAppScenario;
use BaserCore\TestSuite\BcTestCase;
use BcCustomContent\Event\BcCustomContentControllerEventListener;
use BcCustomContent\Service\CustomTablesServiceInterface;
use BcCustomContent\Test\Factory\CustomContentFactory;
use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\Event\EventManager;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;

/**
* Class BcCustomContentControllerEventListenerTest
*
* @property BcCustomContentControllerEventListener $BcCustomContentControllerEventListener
*/
class BcCustomContentControllerEventListenerTest extends BcTestCase
{
/**
* Trait
*/
use ScenarioAwareTrait;

/**
* @var UsersController
*/
public $UsersController;

/**
* @var EventManager|null
*/
public $eventManager;

/**
* setUp
*
* @return void
*/
public function setUp(): void
{
parent::setUp();
$this->loadFixtureScenario(InitAppScenario::class);
$this->eventManager = EventManager::instance();

$this->BcCustomContentControllerEventListener = new BcCustomContentControllerEventListener();
foreach ($this->BcCustomContentControllerEventListener->implementedEvents() as $key => $event) {
$this->eventManager->off($key);
}
$this->UsersController = new UsersController($this->loginAdmin($this->getRequest('/baser/admin/baser-core/users/')));
}

/**
* tearDown
*
* @return void
*/
public function tearDown(): void
{
$this->BcCustomContentControllerEventListener = null;
parent::tearDown();
}

/**
* test startup
*/
public function testStartupAnSetAdminMenu()
{
//データを生成
$dataBaseService = $this->getService(BcDatabaseServiceInterface::class);
$customTable = $this->getService(CustomTablesServiceInterface::class);
//カスタムテーブルとカスタムエントリテーブルを生成
$customTable->create(['id' => 1, 'name' => 'recruit_categories']);
CustomContentFactory::make(['id' => 1, 'custom_table_id' => 1])->persist();
ContentFactory::make([
'plugin' => 'BcCustomContent',
'type' => 'CustomContent',
'site_id' => 1,
'title' => 'サービスタイトル',
'entity_id' => 1,
])->persist();

$listener = $this->getMockBuilder(BcCustomContentControllerEventListener::class)
->onlyMethods(['implementedEvents'])
->addMethods(['usersStartup'])
->getMock();

$listener->method('implementedEvents')
->willReturn(['Controller.BaserCore.Users.startup' => ['callable' => 'usersStartup']]);

$this->eventManager
->on($listener)
->on($this->BcCustomContentControllerEventListener)
->dispatch(new Event('Controller.startup', $this->UsersController, []));

//メーニューにカスタムタイトルがあるか確認
$menu = Configure::read('BcApp.adminNavigation.Contents');
$this->assertEquals('サービスタイトル', $menu['CustomContent1']['title']);
//不要なテーブルを削除
$dataBaseService->dropTable('custom_entry_1_recruit_categories');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BaserCore\Service\BcDatabaseServiceInterface;
use BaserCore\TestSuite\BcTestCase;
use BcBlog\View\BlogAdminAppView;
use BcCustomContent\Model\Entity\CustomEntry;
use BcCustomContent\Service\CustomEntriesServiceInterface;
use BcCustomContent\Service\CustomTablesServiceInterface;
Expand Down Expand Up @@ -511,4 +512,23 @@ public function testError()
$rs = $this->CustomContentAdminHelper->error($customLink, ['parent' => $customLinkParent]);
$this->assertEquals('', $rs);
}

/**
* test displayPluginMeta
*/
public function testDisplayPluginMeta()
{
//準備
$view = new BlogAdminAppView($this->getRequest('/baser/admin'));
$view->loadHelper('BcAdminForm');
$this->CustomContentAdminHelper = new CustomContentAdminHelper($view);

//テスト
ob_start();
$this->CustomContentAdminHelper->displayPluginMeta();
$output = ob_get_clean();

//戻り値を確認
$this->assertTextContains('<label for="">自動補完郵便番号設定</label> ', $output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace BcMail\Test\TestCase\Service\Front;

use BaserCore\Test\Factory\ContentFactory;
use BaserCore\Test\Factory\SiteConfigFactory;
use BaserCore\Test\Scenario\InitAppScenario;
use BaserCore\TestSuite\BcTestCase;
use BaserCore\Utility\BcFile;
Expand All @@ -29,6 +30,7 @@
use BcMail\Test\Scenario\MailContentsScenario;
use BcMail\Test\Scenario\MailFieldsScenario;
use Cake\ORM\Entity;
use Cake\TestSuite\EmailTrait;
use Cake\TestSuite\IntegrationTestTrait;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;
use InvalidArgumentException;
Expand All @@ -46,6 +48,7 @@ class MailFrontServiceTest extends BcTestCase
*/
use ScenarioAwareTrait;
use IntegrationTestTrait;
use EmailTrait;

/**
* set up
Expand Down Expand Up @@ -483,14 +486,14 @@ public function test_getIndexTemplate()

/**
* test sendMail
* @throws \Throwable
*/
public function test_sendMail()
{
// prepare
$this->loadFixtureScenario(InitAppScenario::class);
$this->loadFixtureScenario(MailContentsScenario::class);
$this->loadFixtureScenario(MailFieldsScenario::class);
SiteConfigFactory::make(['name' => 'email', 'value' => 'basertest@example.com'])->persist();
$MailContentsService = $this->getService(MailContentsServiceInterface::class);
$MailMessagesService = $this->getService(MailMessagesServiceInterface::class);
MailMessagesFactory::make(
Expand Down Expand Up @@ -525,8 +528,9 @@ public function test_sendMail()
])->persist();
// normal case
$mailContent = $MailContentsService->get(99);
$this->expectException(InvalidArgumentException::class);
$this->MailFrontService->sendMail($mailContent, $mailMessage, []);
$this->assertMailSentTo('t@gm.com');
$this->assertMailSubjectContains('お問い合わせを頂きました99');
}

/**
Expand Down

0 comments on commit 7541e39

Please sign in to comment.