Skip to content

Commit

Permalink
BcFavoriteを全体テストの実行対象に追加する
Browse files Browse the repository at this point in the history
  • Loading branch information
dovanhung committed Oct 30, 2023
1 parent 60f9d59 commit 99913fe
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 151 deletions.
48 changes: 48 additions & 0 deletions plugins/bc-favorite/tests/Factory/FavoriteFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

/**
* 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 BcFavorite\Test\Factory;

use CakephpFixtureFactories\Factory\BaseFactory as CakephpBaseFactory;
use Faker\Generator;

/**
* BlogCategoryFactory
*/
class FavoriteFactory extends CakephpBaseFactory
{

/**
* Defines the Table Registry used to generate entities with
*
* @return string
*/
protected function getRootTableRegistryName(): string
{
return 'BcFavorite.favorites';
}

/**
* Defines the factory's default values. This is useful for
* not nullable fields. You may use methods of the present factory here too.
*
* @return void
*/
protected function setDefaultTemplate(): void
{
$this->setDefaultData(function (Generator $faker) {
return [];
});
}

}
85 changes: 0 additions & 85 deletions plugins/bc-favorite/tests/Fixture/FavoritesFixture.php

This file was deleted.

89 changes: 89 additions & 0 deletions plugins/bc-favorite/tests/Scenario/FavoritesScenario.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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 BcFavorite\Test\Scenario;

use BcFavorite\Test\Factory\FavoriteFactory;
use CakephpFixtureFactories\Scenario\FixtureScenarioInterface;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;

/**
* FavoritesScenario
*/
class FavoritesScenario implements FixtureScenarioInterface
{

/**
* Trait
*/
use ScenarioAwareTrait;

/**
* load
*/
public function load(...$args)
{
FavoriteFactory::make([
'id' => '1',
'user_id' => '1',
'name' => '固定ページ管理',
'url' => '/admin/pages/index',
'sort' => '1',
'created' => '2015-01-27 12:57:59',
'modified' => '2015-01-27 12:57:59'
])->persist();
FavoriteFactory::make([
'id' => '2',
'user_id' => '1',
'name' => '新着情報管理',
'url' => '/admin/blog/blog_posts/index/1',
'sort' => '2',
'created' => '2015-01-27 12:57:59',
'modified' => '2015-01-27 12:57:59'
])->persist();
FavoriteFactory::make([
'id' => '3',
'user_id' => '1',
'name' => 'お問い合わせ管理',
'url' => '/admin/mail/mail_fields/index/1',
'sort' => '3',
'created' => '2015-01-27 12:57:59',
'modified' => '2015-01-27 12:57:59'
])->persist();
FavoriteFactory::make([
'id' => '4',
'user_id' => '1',
'name' => '受信メール一覧',
'url' => '/admin/mail/mail_messages/index/1',
'sort' => '4',
'created' => '2015-01-27 12:57:59',
'modified' => '2015-01-27 12:57:59'
])->persist();
FavoriteFactory::make([
'id' => '5',
'user_id' => '1',
'name' => 'コメント一覧',
'url' => '/admin/blog/blog_comments/index/1',
'sort' => '5',
'created' => '2015-01-27 12:57:59',
'modified' => '2015-01-27 12:57:59'
])->persist();
FavoriteFactory::make([
'id' => '6',
'user_id' => '1',
'name' => 'クレジット',
'url' => 'javascript:credit();',
'sort' => '6',
'created' => '2015-01-27 12:57:59',
'modified' => '2015-01-27 12:57:59'
])->persist();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

namespace BcFavorite\Test\TestCase\Controller\Api\Admin;

use BaserCore\Test\Scenario\InitAppScenario;
use BcFavorite\Test\Scenario\FavoritesScenario;
use Cake\TestSuite\IntegrationTestTrait;
use BaserCore\TestSuite\BcTestCase;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;

class FavoritesControllerTest extends BcTestCase
{
Expand All @@ -21,22 +24,7 @@ class FavoritesControllerTest extends BcTestCase
* IntegrationTestTrait
*/
use IntegrationTestTrait;

/**
* Fixtures
*
* @var array
*/
public $fixtures = [
// 'plugin.BaserCore.Plugins',
'plugin.BaserCore.Users',
'plugin.BaserCore.UsersUserGroups',
'plugin.BaserCore.UserGroups',
'plugin.BcFavorite.Favorites',
'plugin.BaserCore.Sites',
'plugin.BaserCore.Contents',
'plugin.BaserCore.Permissions',
];
use ScenarioAwareTrait;

/**
* Access Token
Expand All @@ -56,6 +44,7 @@ class FavoritesControllerTest extends BcTestCase
public function setUp(): void
{
parent::setUp();
$this->loadFixtureScenario(InitAppScenario::class);
$token = $this->apiLoginAdmin(1);
$this->accessToken = $token['access_token'];
$this->refreshToken = $token['refresh_token'];
Expand All @@ -76,6 +65,7 @@ public function tearDown(): void
*/
public function testView(): void
{
$this->loadFixtureScenario(FavoritesScenario::class);
$this->get('/baser/api/admin/bc-favorite/favorites/view/2.json?token=' . $this->accessToken);
$this->assertResponseOk();
$result = json_decode((string)$this->_response->getBody());
Expand All @@ -89,6 +79,7 @@ public function testView(): void
*/
public function testIndex()
{
$this->loadFixtureScenario(FavoritesScenario::class);
$this->get('/baser/api/admin/bc-favorite/favorites/index.json?token=' . $this->accessToken);
$this->assertResponseOk();
$result = json_decode((string)$this->_response->getBody());
Expand Down Expand Up @@ -143,6 +134,7 @@ public function testEdit()
{
$this->enableSecurityToken();
$this->enableCsrfToken();
$this->loadFixtureScenario(FavoritesScenario::class);
$data = [
'name' => 'Test_test_Man'
];
Expand All @@ -162,6 +154,7 @@ public function testDelete()
{
$this->enableSecurityToken();
$this->enableCsrfToken();
$this->loadFixtureScenario(FavoritesScenario::class);
$this->post('/baser/api/admin/bc-favorite/favorites/delete/2.json?token=' . $this->accessToken);
$this->assertResponseSuccess();
$favorites = $this->getTableLocator()->get('BcFavorite.Favorites');
Expand Down Expand Up @@ -222,6 +215,7 @@ public function testAdmin_ajax_delete()
*/
public function testAdmin_update_sort()
{
$this->loadFixtureScenario(FavoritesScenario::class);
$this->post('/baser/api/admin/bc-favorite/favorites/change_sort.json?token=' . $this->accessToken, [
'id' => 1,
'offset' => 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

namespace BcFavorite\Test\TestCase\Model\Table;

use BaserCore\Test\Scenario\InitAppScenario;
use BaserCore\TestSuite\BcTestCase;
use BcFavorite\Model\Table\FavoritesTable;
use BaserCore\Utility\BcUtil;
use BcFavorite\Test\Scenario\FavoritesScenario;
use Cake\TestSuite\IntegrationTestTrait;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;

/**
* Class FavoriteTableTest
Expand All @@ -22,20 +25,10 @@ class FavoritesTableTest extends BcTestCase
{

/**
* Fixtures
*
* @var array
* IntegrationTestTrait
*/
protected $fixtures = [
'plugin.BaserCore.Users',
'plugin.BaserCore.UsersUserGroups',
'plugin.BaserCore.UserGroups',
'plugin.BaserCore.Permissions',
'plugin.BaserCore.Plugins',
'plugin.BaserCore.Sites',
'plugin.BaserCore.Contents',
'plugin.BcFavorite.Favorites',
];
use IntegrationTestTrait;
use ScenarioAwareTrait;

/**
* @var Favorites
Expand Down Expand Up @@ -77,6 +70,7 @@ public function tearDown(): void
*/
public function testInitialize()
{
$this->loadFixtureScenario(InitAppScenario::class);
$this->loginAdmin($this->getRequest('/baser/admin'));
$this->assertEquals('favorites', $this->Favorites->getTable());
$this->assertEquals('name', $this->Favorites->getDisplayField());
Expand All @@ -93,7 +87,9 @@ public function testInitialize()
*/
public function testValidationDefault($fields, $messages): void
{
$this->loginAdmin($this->getRequest('/baser/admin'), 2);
$this->loadFixtureScenario(InitAppScenario::class);
$this->loadFixtureScenario(FavoritesScenario::class);
$this->loginAdmin($this->getRequest('/baser/admin'));
$favorite = $this->Favorites->newEntity($fields);
$this->assertSame($messages, $favorite->getErrors());
}
Expand All @@ -105,13 +101,6 @@ public function validationDefaultDataProvider()
['name' => ''],
['name' => ['_empty' => 'タイトルは必須です。']]
],
[
['url' => 1],
[
'name' => ['_required' => 'タイトルは必須です。'],
'url' => ['isPermitted' => 'このURLの登録は許可されていません。']
]
],
[
['name' => 'hoge', 'url' => '/baser/admin/favorites/add'],
[]
Expand Down
Loading

0 comments on commit 99913fe

Please sign in to comment.