Skip to content

Commit

Permalink
Merge branch 'dev-5.1' into unitTest_BlogHelper_testGetCurrentBlogTag
Browse files Browse the repository at this point in the history
  • Loading branch information
thangnn committed Feb 26, 2024
2 parents 98c040e + 16e6519 commit 358cb63
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 7 deletions.
25 changes: 25 additions & 0 deletions plugins/baser-core/src/Model/Table/PermissionGroupsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use BaserCore\Annotation\UnitTest;
use BaserCore\Annotation\NoTodo;
use BaserCore\Annotation\Checked;
use Cake\Validation\Validator;

class PermissionGroupsTable extends AppTable
{
Expand Down Expand Up @@ -43,4 +44,28 @@ public function initialize(array $config): void
]);
}

/**
* Validation Default
*
* @param Validator $validator
* @return Validator
* @checked
* @noTodo
* @unitTest
*/
public function validationDefault(Validator $validator): Validator
{
// id
$validator
->integer('id')
->allowEmptyString('id', null, 'create');

$validator
->scalar('name')
->maxLength('name', 60, __d('baser_core', 'ルールグループ名は60文字以内で入力してください。'))
->notEmptyString('name', __d('baser_core', 'ルールグループ名を入力してください。'));

return $validator;
}

}
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\Model\Table;

use BaserCore\Model\Table\PermissionGroupsTable;
use BaserCore\TestSuite\BcTestCase;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;

/**
* Class PermissionGroupsTableTest
* @property PermissionGroupsTable $PermissionGroupsTable
*/
class PermissionsGroupsTableTest extends BcTestCase
{
/**
* ScenarioAwareTrait
*/
use ScenarioAwareTrait;

/**
* Set Up
*
* @return void
*/
public function setUp(): void
{
parent::setUp();
$this->PermissionGroupsTable = $this->getTableLocator()->get('BaserCore.PermissionGroups');
}

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

/**
* Test initialize
*/
public function testInitialize()
{
$this->assertEquals('permission_groups', $this->PermissionGroupsTable->getTable());
$this->assertEquals('name', $this->PermissionGroupsTable->getDisplayField());
$this->assertEquals('id', $this->PermissionGroupsTable->getPrimaryKey());
$this->assertIsBool($this->PermissionGroupsTable->hasBehavior('Timestamp'));
$this->assertEquals('Permissions', $this->PermissionGroupsTable->getAssociation('Permissions')->getName());
}

/**
* Test validationDefault
*
* @return void
*/
public function testValidationDefault()
{
$validator = $this->PermissionGroupsTable->getValidator('default');
$errors = $validator->validate([
'name' => '',
]);

$this->assertEquals('ルールグループ名を入力してください。', current($errors['name']));

$validator = $this->PermissionGroupsTable->getValidator('default');
$errors = $validator->validate([
'name' => str_repeat('a', 61),
]);

$this->assertEquals('ルールグループ名は60文字以内で入力してください。', current($errors['name']));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @noTodo
* @unitTest
*/
$this->BcAdmin->setTitle(__d('baser_core', 'データメンテナンス'));
$this->BcAdmin->setTitle(__d('baser_core', 'ログメンテナンス'));
$this->BcAdmin->setHelp('tools_log');
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
</th>
<td class="col-input bca-form-table__input">
<?php echo $this->BcAdminForm->control('name', ['type' => 'text','size' => 60]) ?>
<?php echo $this->BcAdminForm->error('name') ?>
</td>
</tr>

Expand Down
6 changes: 4 additions & 2 deletions plugins/bc-content-link/src/Model/Table/ContentLinksTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ public function validationDefault(Validator $validator): Validator
->requirePresence('id', 'update');

$validator
->scalar('url')
->notEmptyString('url', __d('baser_core', 'リンク先URLを入力してください。'), 'update');
->scalar('url')
->notEmptyString('url', __d('baser_core', 'リンク先URLを入力してください。'), 'update')
->regex('url', '/^http|^\/.*/', __d('baser_core', 'リンク先URLはURLの形式を入力してください。'))
->maxLength('url', 255, __d('baser_core', 'リンク先URLは255文字以内で入力してください。'));

return $validator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,47 @@ public function testValidationDefault()
], $contentLink->getErrors());
}

/**
* Test testValidationURL
*/
public function testValidationURL()
{
$validator = $this->ContentLinks->getValidator('default');
//エラー場合、
//スペースだけ入力
$errors = $validator->validate([
'url' => ' '
]);
//戻り値を確認
$this->assertEquals('リンク先URLはURLの形式を入力してください。', current($errors['url']));
//スラッシュがない場合
$errors = $validator->validate([
'url' => 'あああああ'
]);
//戻り値を確認
$this->assertEquals('リンク先URLはURLの形式を入力してください。', current($errors['url']));

//長いURLを入力場合
$errors = $validator->validate([
'url' => '/' . str_repeat('a', 255)
]);
//戻り値を確認
$this->assertEquals('リンク先URLは255文字以内で入力してください。', current($errors['url']));

//正常場合、
$errors = $validator->validate([
'url' => '/test'
]);
//戻り値を確認
$this->assertCount(0, $errors);

$errors = $validator->validate([
'url' => 'https://basercms.net/'
]);
//戻り値を確認
$this->assertCount(0, $errors);
}

/**
* test beforeCopyEvent
*/
Expand All @@ -88,13 +129,13 @@ public function testBeforeCopyEvent()
//イベントをコル
$this->entryEventToMock(self::EVENT_LAYER_MODEL, 'BcContentLink.ContentLinks.beforeCopy', function (Event $event) {
$data = $event->getData('data');
$data['url'] = 'beforeCopy';
$data['url'] = '/beforeCopy';
$event->setData('data', $data);
});
$this->ContentLinks->copy(1, 1, 'new title', 1, 1);
//イベントに入るかどうか確認
$contentLinks = $this->getTableLocator()->get('BcContentLink.ContentLinks');
$query = $contentLinks->find()->where(['url' => 'beforeCopy']);
$query = $contentLinks->find()->where(['url' => '/beforeCopy']);
$this->assertEquals(1, $query->count());
}

Expand All @@ -108,13 +149,13 @@ public function testAfterCopyEvent()
$this->entryEventToMock(self::EVENT_LAYER_MODEL, 'BcContentLink.ContentLinks.afterCopy', function (Event $event) {
$data = $event->getData('data');
$contentLinks = TableRegistry::getTableLocator()->get('BcContentLink.ContentLinks');
$data->url = 'AfterCopy';
$data->url = '/AfterCopy';
$contentLinks->save($data);
});
$this->ContentLinks->copy(1, 1, 'new title', 1, 1);
//イベントに入るかどうか確認
$contentLinks = $this->getTableLocator()->get('BcContentLink.ContentLinks');
$query = $contentLinks->find()->where(['url' => 'AfterCopy']);
$query = $contentLinks->find()->where(['url' => '/AfterCopy']);
$this->assertEquals(1, $query->count());
}

Expand Down

0 comments on commit 358cb63

Please sign in to comment.