Skip to content

Commit

Permalink
Merge branch '5.1.x' into unitTest_MailHelper_link
Browse files Browse the repository at this point in the history
  • Loading branch information
thangnn committed Dec 10, 2024
2 parents c386503 + 8522806 commit f16f06d
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function initialize(): void
* @return void
* @checked
* @noTodo
* @unitTest
*/
public function beforeFilter(EventInterface $event)
{
Expand All @@ -80,6 +81,7 @@ public function beforeFilter(EventInterface $event)
* @return void
* @checked
* @noTodo
* @unitTest
*/
public function index(MailMessagesAdminServiceInterface $service, int $mailContentId)
{
Expand Down
1 change: 1 addition & 0 deletions plugins/bc-mail/src/Model/Table/MailMessagesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public function createTableName(int $mailContentId): string
* アップロード設定を行う
* @checked
* @noTodo
* @unitTest
*/
public function setupUpload(int $mailContentId)
{
Expand Down
1 change: 1 addition & 0 deletions plugins/bc-mail/src/View/Helper/MailformHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ public function control(string $fieldName, array $attributes = []): string
* @return string
* @checked
* @noTodo
* @unitTest
*/
public function create($context = null, $options = []): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@

namespace BcMail\Test\TestCase\Controller\Admin;

use BaserCore\Test\Factory\ContentFactory;
use BaserCore\Test\Scenario\InitAppScenario;
use BaserCore\TestSuite\BcTestCase;
use BcMail\Controller\Admin\MailMessagesController;
use BcMail\Service\MailMessagesServiceInterface;
use BcMail\Test\Factory\MailContentFactory;
use Cake\Event\Event;
use Cake\TestSuite\IntegrationTestTrait;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;

class MailMessagesControllerTest extends BcTestCase
{

/**
* Trait
*/
use ScenarioAwareTrait;
use IntegrationTestTrait;
/**
* set up
*
Expand All @@ -25,7 +36,8 @@ class MailMessagesControllerTest extends BcTestCase
public function setUp(): void
{
parent::setUp();
$this->MailMessagesController = new MailMessagesController($this->getRequest());
$this->loadFixtureScenario(InitAppScenario::class);
$this->MailMessagesController = new MailMessagesController($this->loginAdmin($this->getRequest()));
}

/**
Expand Down Expand Up @@ -53,7 +65,31 @@ public function testInitialize()
*/
public function testBeforeFilter()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
ContentFactory::make([
'name' => 'name_test',
'plugin' => 'BcMail',
'type' => 'MailContent',
'url' => '/contact/',
'site_id' => 1,
'title' => 'お問い合わせ',
'entity_id' => 1
])->persist();

//正常テスト・エラーにならない
$request = $this->getRequest('/baser/admin/bc-mail/mail_messages/view/1/1');
$request = $this->loginAdmin($request);
$this->MailMessagesController = new MailMessagesController($request);
$event = new Event('filter');
$this->MailMessagesController->beforeFilter($event);

//異常テスト
$request = $this->getRequest('/baser/admin/bc-mail/mail_messages/view/2222/1');
$request = $this->loginAdmin($request);
$this->MailMessagesController = new MailMessagesController($request);
$event = new Event('filter');
$this->expectExceptionMessage('コンテンツデータが見つかりません。');
$this->expectException('BaserCore\Error\BcException');
$this->MailMessagesController->beforeFilter($event);
}

/**
Expand All @@ -69,7 +105,26 @@ public function testBeforeRender()
*/
public function testIndex()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
$this->enableSecurityToken();
$this->enableCsrfToken();
// メールメッセージのデータを作成する
ContentFactory::make([
'plugin' => 'BcMail',
'type' => 'MailContent',
'url' => '/contact/',
'site_id' => 1,
'title' => 'お問い合わせ',
'entity_id' => 1,
])->persist();
MailContentFactory::make(['id' => 1])->persist();
$MailMessagesService = $this->getService(MailMessagesServiceInterface::class);
//テストデータベースを生成
$MailMessagesService->createTable(1);

//正常テスト
$this->get('/baser/admin/bc-mail/mail_messages/index/1');
$this->assertResponseOk();
$MailMessagesService->dropTable(1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ public function testSetUseTable()
*/
public function testSetupUpload()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
$this->MailMessage->setupUpload(1);
$bcUpload = $this->MailMessage->getBehavior('BcUpload');
$this->assertEquals('/var/www/html/webroot/files/mail/limited/1/messages/', $bcUpload->BcFileUploader["MailMessages"]->savePath);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use BaserCore\TestSuite\BcTestCase;
use BcMail\Model\Entity\MailField;
use BcMail\Test\Factory\MailMessagesFactory;
use BcMail\View\Helper\MailformHelper;
use Cake\ORM\ResultSet;
use Cake\View\View;
Expand Down Expand Up @@ -50,7 +51,11 @@ public function testControl()
*/
public function testCreate()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
$rs = $this->MailformHelper->create();
$this->assertTextContains('form enctype="multipart/form-data"', $rs);

$rs = $this->MailformHelper->create(MailMessagesFactory::make()->getEntity(), ['url' => '/test']);
$this->assertTextContains('action="/test"', $rs);
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public function bootstrap(): void
*/
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{
$csrfProtectionMiddlewareOptions = ['httponly' => true];
//リクエストがhttpsならcsrfTokenにもsecureヘッダを付与
$sessionConfig = (array) Configure::read('Session');
if (!empty($sessionConfig['ini']['session.cookie_secure']) || (int) ini_get('session.cookie_secure') === 1) {
$csrfProtectionMiddlewareOptions['secure'] = true;
}
$middlewareQueue
// Catch any exceptions in the lower layers,
// and make an error page/response
Expand All @@ -87,9 +93,7 @@ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue

// Cross Site Request Forgery (CSRF) Protection Middleware
// https://book.cakephp.org/5/en/security/csrf.html#cross-site-request-forgery-csrf-middleware
->add(new CsrfProtectionMiddleware([
'httponly' => true,
]));
->add(new CsrfProtectionMiddleware($csrfProtectionMiddlewareOptions));

return $middlewareQueue;
}
Expand Down

0 comments on commit f16f06d

Please sign in to comment.