diff --git a/Config/Migration/NetCommonsMigration.php b/Config/Migration/NetCommonsMigration.php index 585e3612..8f46dbab 100644 --- a/Config/Migration/NetCommonsMigration.php +++ b/Config/Migration/NetCommonsMigration.php @@ -90,6 +90,7 @@ public function generateModel($name, $table = null, $options = array()) { * @param bool $clear 初期化するかどうか * @return bool Should process continue * @SuppressWarnings(PHPMD.BooleanArgumentFlag) + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function updateRecords($model, $argRecords, $clear = false) { if (isset($argRecords['class']) && isset($argRecords['records'])) { diff --git a/Test/Case/Utility/CurrentFrame/SetBlockTest.php b/Test/Case/Utility/CurrentFrame/SetBlockTest.php index 70e04458..21dad4ae 100644 --- a/Test/Case/Utility/CurrentFrame/SetBlockTest.php +++ b/Test/Case/Utility/CurrentFrame/SetBlockTest.php @@ -96,7 +96,7 @@ public function testWithFrame() { 'Room' => array( 'id' => '2', 'space_id' => '2', - 'page_id_top' => '1', + 'page_id_top' => '4', 'parent_id' => '1', 'lft' => '2', 'rght' => '7', diff --git a/Test/Case/Utility/CurrentFrame/SetFrameTest.php b/Test/Case/Utility/CurrentFrame/SetFrameTest.php index acecb330..623cfbe7 100644 --- a/Test/Case/Utility/CurrentFrame/SetFrameTest.php +++ b/Test/Case/Utility/CurrentFrame/SetFrameTest.php @@ -442,7 +442,7 @@ private function __assertFrameMain($mergeExpected = array(), $removeKeys = array 'type' => '4', 'space_id' => '2', 'room_id' => '2', - 'page_id' => '2', + 'page_id' => '4', 'container_type' => '3', 'weight' => null, ), @@ -474,7 +474,7 @@ private function __assertFrameMain($mergeExpected = array(), $removeKeys = array 'Room' => array( 'id' => '2', 'space_id' => '2', - 'page_id_top' => '1', + 'page_id_top' => '4', 'parent_id' => '1', 'lft' => '2', 'rght' => '7', @@ -502,31 +502,31 @@ private function __assertFrameMain($mergeExpected = array(), $removeKeys = array 'after_user_save_model' => null, ), 'BoxesPageContainer' => array( - 'id' => '50', - 'page_container_id' => '8', - 'page_id' => '2', + 'id' => '54', + 'page_container_id' => '18', + 'page_id' => '4', 'container_type' => '3', 'box_id' => '28', 'is_published' => true, 'weight' => '1', ), 'PageContainer' => array( - 'id' => '8', - 'page_id' => '2', + 'id' => '18', + 'page_id' => '4', 'container_type' => '3', 'is_published' => true, 'is_configured' => false, ), 'Page' => array( - 'id' => '2', + 'id' => '4', 'room_id' => '2', 'root_id' => '1', 'parent_id' => '1', 'lft' => '2', - 'rght' => '3', + 'rght' => '5', 'permalink' => 'home', 'slug' => 'home', - 'is_container_fluid' => true, + 'is_container_fluid' => false, 'theme' => null, ), ); diff --git a/Test/Case/Utility/CurrentPage/SetPageTest.php b/Test/Case/Utility/CurrentPage/SetPageTest.php new file mode 100644 index 00000000..1a9761dc --- /dev/null +++ b/Test/Case/Utility/CurrentPage/SetPageTest.php @@ -0,0 +1,583 @@ + + * @author Shohei Nakajima + * @link http://www.netcommons.org NetCommons Project + * @license http://www.netcommons.org/license.txt NetCommons License + * @copyright Copyright 2014, NetCommons Project + */ + +App::uses('NetCommonsCakeTestCase', 'NetCommons.TestSuite'); +App::uses('NetCommonsCurrentUtilityBase', 'NetCommons.TestSuite'); +App::uses('CurrentPage', 'NetCommons.Utility'); + +/** + * CurrentPage::setPage()のテスト + * + * @author Shohei Nakajima + * @package NetCommons\NetCommons\Test\Case\Utility\CurrentPage + * @SuppressWarnings(PHPMD.TooManyPublicMethods) + */ +class NetCommonsUtilityCurrentPageSetPageTest extends NetCommonsCurrentUtilityBase { + +/** + * Plugin name + * + * @var string + */ + public $plugin = 'net_commons'; + +/** + * setUp method + * + * @return void + */ + public function setUp() { + parent::setUp(); + + $this->CurrentPage = new CurrentPage(); + Current::$current['Language']['id'] = '2'; + } + +/** + * tearDown method + * + * @return void + */ + public function tearDown() { + unset($this->CurrentPage); + parent::tearDown(); + } + +/** + * page_idの指定なし + * + * @return void + */ + public function testNoPageIdWOPlugin() { + //データ生成 + + //テスト実施 + $this->CurrentPage->setPage(); + + //チェック + $expected = array( + 'Language' => array( + 'id' => '2', + ), + ); + $this->assertEquals(Current::$current, $expected); + } + +/** + * page_idの指定なしでプラグイン指定 + * + * @return void + */ + public function testNoPageIdWithPlugin() { + //データ生成 + Current::$request->params['plugin'] = 'blogs'; + + //テスト実施 + $this->CurrentPage->setPage(); + + //チェック + $this->__assertCurrent(); + } + +/** + * Current::$request->dataにPage.idがある + * + * @return void + */ + public function testRequestData() { + //データ生成 + Current::$request->data['Page']['id'] = '4'; + + //テスト実施 + $this->CurrentPage->setPage(); + + //チェック + $this->__assertCurrent(); + } + +/** + * pagesプラグインのpagesコントローラで、ホームの場合 + * + * @return void + */ + public function testPagesRoot() { + //データ生成 + Current::$request->params['plugin'] = 'pages'; + Current::$request->params['controller'] = 'pages'; + Current::$request->params['pass'] = []; + Current::$request->params['pageView'] = true; + + //テスト実施 + $this->CurrentPage->setPage(); + + //チェック + $this->__assertCurrent(); + } + +/** + * pagesプラグインのpagesコントローラで、permalinkあり + * + * @return void + */ + public function testPagesWithPermalink() { + //データ生成 + Current::$request->params['plugin'] = 'pages'; + Current::$request->params['controller'] = 'pages'; + Current::$request->params['pass'] = ['test4']; + Current::$request->params['pageView'] = true; + + //テスト実施 + $this->CurrentPage->setPage(); + + //チェック + $expected = [ + 'Page' => [ + 'id' => '7', + 'room_id' => '2', + 'root_id' => '1', + 'parent_id' => '4', + 'lft' => '3', + 'rght' => '4', + 'permalink' => 'test4', + 'slug' => 'test4', + 'full_permalink' => 'test4', + ], + 'ParentPage' => [ + 'id' => '4', + 'room_id' => '2', + 'root_id' => '1', + 'parent_id' => '1', + 'lft' => '2', + 'rght' => '5', + 'permalink' => 'home', + 'slug' => 'home', + 'is_container_fluid' => false, + 'theme' => null, + ] + ]; + $this->__assertCurrent($expected); + } + +/** + * pagesプラグインのpages_editコントローラで、page_idあり + * + * @return void + */ + public function testPagesEditWithPageId() { + //データ生成 + Current::$request->params['plugin'] = 'pages'; + Current::$request->params['controller'] = 'pages_edit'; + Current::$request->params['pass'] = ['2', '4']; + Current::$request->params['pageEdit'] = true; + + //テスト実施 + $this->CurrentPage->setPage(); + + //チェック + $this->__assertCurrent(); + } + +/** + * pagesプラグインのpages_editコントローラで、page_idなし + * + * @return void + */ + public function testPagesEditWOPageId() { + //データ生成 + Current::$request->params['plugin'] = 'pages'; + Current::$request->params['controller'] = 'pages_edit'; + Current::$request->params['pass'] = ['2']; + Current::$request->params['pageEdit'] = true; + + //テスト実施 + $this->CurrentPage->setPage(); + + //チェック + $expected = [ + 'ParentRoom' => [ + 'id' => '1', + 'space_id' => '1', + 'page_id_top' => null, + 'parent_id' => null, + 'lft' => '1', + 'rght' => '12', + 'active' => true, + 'in_draft' => false, + 'default_role_key' => 'visitor', + 'need_approval' => true, + 'default_participation' => true, + 'page_layout_permitted' => false, + 'theme' => null, + ], + ]; + $this->__assertCurrent($expected); + } + +/** + * Current::$request->queryにpage_idがある + * + * @return void + */ + public function testQueryWithPageId() { + //データ生成 + Current::$request->query['page_id'] = '4'; + + //テスト実施 + $this->CurrentPage->setPage(); + + //チェック + $this->__assertCurrent(); + } + +/** + * usersプラグイン + * + * @return void + */ + public function testUsersPlugin() { + //データ生成 + Current::$request->params['plugin'] = 'users'; + Current::$current['User']['id'] = '1'; + + //テスト実施 + $this->CurrentPage->setPage(); + + //チェック + $expected = [ + 'User' => [ + 'id' => '1', + ], + 'Room' => [ + 'id' => '7', + 'space_id' => '3', + 'page_id_top' => '10', + 'parent_id' => '3', + 'lft' => '10', + 'rght' => '11', + 'active' => true, + 'in_draft' => false, + 'default_role_key' => 'room_administrator', + 'need_approval' => false, + 'default_participation' => false, + 'page_layout_permitted' => false, + 'theme' => null, + ], + 'RolesRoomsUser' => [ + 'id' => '12', + 'roles_room_id' => '16', + 'user_id' => '1', + 'room_id' => '7', + 'access_count' => '0', + 'last_accessed' => null, + 'previous_accessed' => null, + ], + 'Page' => [ + 'id' => '10', + 'room_id' => '7', + 'root_id' => '2', + 'parent_id' => '2', + 'lft' => '10', + 'rght' => '11', + 'permalink' => 'user_administrator', + 'slug' => 'user_administrator', + 'is_container_fluid' => false, + 'theme' => null, + 'full_permalink' => 'private/user_administrator', + ], + 'ParentPage' => [ + 'id' => '2', + 'room_id' => '3', + 'root_id' => null, + 'parent_id' => null, + 'lft' => '9', + 'rght' => '12', + 'permalink' => '', + 'slug' => null, + 'is_container_fluid' => false, + 'theme' => null, + ], + 'Space' => [ + 'id' => '3', + 'parent_id' => '1', + 'lft' => '4', + 'rght' => '5', + 'type' => '3', + 'plugin_key' => 'private_space', + 'default_setting_action' => '', + 'room_disk_size' => null, + 'room_id_root' => '3', + 'page_id_top' => '2', + 'permalink' => 'private', + 'is_m17n' => false, + 'after_user_save_model' => null, + ], + ]; + $this->__assertCurrent($expected); + } + +/** + * groupsプラグイン + * + * @return void + */ + public function testGroupsPlugin() { + //データ生成 + Current::$request->params['plugin'] = 'groups'; + Current::$current['User']['id'] = '1'; + + //テスト実施 + $this->CurrentPage->setPage(); + + //チェック + $expected = [ + 'User' => [ + 'id' => '1', + ], + 'Room' => [ + 'id' => '7', + 'space_id' => '3', + 'page_id_top' => '10', + 'parent_id' => '3', + 'lft' => '10', + 'rght' => '11', + 'active' => true, + 'in_draft' => false, + 'default_role_key' => 'room_administrator', + 'need_approval' => false, + 'default_participation' => false, + 'page_layout_permitted' => false, + 'theme' => null, + ], + 'RolesRoomsUser' => [ + 'id' => '12', + 'roles_room_id' => '16', + 'user_id' => '1', + 'room_id' => '7', + 'access_count' => '0', + 'last_accessed' => null, + 'previous_accessed' => null, + ], + 'Page' => [ + 'id' => '10', + 'room_id' => '7', + 'root_id' => '2', + 'parent_id' => '2', + 'lft' => '10', + 'rght' => '11', + 'permalink' => 'user_administrator', + 'slug' => 'user_administrator', + 'is_container_fluid' => false, + 'theme' => null, + 'full_permalink' => 'private/user_administrator', + ], + 'ParentPage' => [ + 'id' => '2', + 'room_id' => '3', + 'root_id' => null, + 'parent_id' => null, + 'lft' => '9', + 'rght' => '12', + 'permalink' => '', + 'slug' => null, + 'is_container_fluid' => false, + 'theme' => null, + ], + 'Space' => [ + 'id' => '3', + 'parent_id' => '1', + 'lft' => '4', + 'rght' => '5', + 'type' => '3', + 'plugin_key' => 'private_space', + 'default_setting_action' => '', + 'room_disk_size' => null, + 'room_id_root' => '3', + 'page_id_top' => '2', + 'permalink' => 'private', + 'is_m17n' => false, + 'after_user_save_model' => null, + ], + ]; + $this->__assertCurrent($expected); + } + +/** + * Current::$current['Room']がある + * + * @return void + */ + public function testWithRoom() { + //データ生成 + Current::$current['Room'] = [ + 'id' => '7', + 'space_id' => '3', + 'page_id_top' => '10', + 'parent_id' => '3', + 'lft' => '10', + 'rght' => '11', + 'active' => true, + 'in_draft' => false, + 'default_role_key' => 'room_administrator', + 'need_approval' => false, + 'default_participation' => false, + 'page_layout_permitted' => false, + 'theme' => null, + ]; + + //テスト実施 + $this->CurrentPage->setPage(); + + //チェック + $expected = [ + 'Room' => [ + 'id' => '7', + 'space_id' => '3', + 'page_id_top' => '10', + 'parent_id' => '3', + 'lft' => '10', + 'rght' => '11', + 'active' => true, + 'in_draft' => false, + 'default_role_key' => 'room_administrator', + 'need_approval' => false, + 'default_participation' => false, + 'page_layout_permitted' => false, + 'theme' => null, + ], + 'Page' => [ + 'id' => '10', + 'room_id' => '7', + 'root_id' => '2', + 'parent_id' => '2', + 'lft' => '10', + 'rght' => '11', + 'permalink' => 'user_administrator', + 'slug' => 'user_administrator', + 'is_container_fluid' => false, + 'theme' => null, + 'full_permalink' => 'private/user_administrator', + ], + 'ParentPage' => [ + 'id' => '2', + 'room_id' => '3', + 'root_id' => null, + 'parent_id' => null, + 'lft' => '9', + 'rght' => '12', + 'permalink' => '', + 'slug' => null, + 'is_container_fluid' => false, + 'theme' => null, + ], + 'Space' => [ + 'id' => '3', + 'parent_id' => '1', + 'lft' => '4', + 'rght' => '5', + 'type' => '3', + 'plugin_key' => 'private_space', + 'default_setting_action' => '', + 'room_disk_size' => null, + 'room_id_root' => '3', + 'page_id_top' => '2', + 'permalink' => 'private', + 'is_m17n' => false, + 'after_user_save_model' => null, + ], + ]; + $this->__assertCurrent($expected); + } + +/** + * Current::$currenの評価 + * + * @param array $mergeExpected $expectedにマージするデータ + * @param array $removeKeys $expectedから削除するデータ + * @return void + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + private function __assertCurrent($mergeExpected = array(), $removeKeys = array()) { + Current::$current = Hash::remove(Current::$current, '{s}.created_user'); + Current::$current = Hash::remove(Current::$current, '{s}.created'); + Current::$current = Hash::remove(Current::$current, '{s}.modified_user'); + Current::$current = Hash::remove(Current::$current, '{s}.modified'); + + $default = array( + 'Language' => array( + 'id' => '2', + ), + 'Page' => array( + 'id' => '4', + 'room_id' => '2', + 'root_id' => '1', + 'parent_id' => '1', + 'lft' => '2', + 'rght' => '5', + 'permalink' => 'home', + 'slug' => 'home', + 'is_container_fluid' => false, + 'theme' => null, + 'full_permalink' => 'home', + ), + 'Room' => array( + 'id' => '2', + 'space_id' => '2', + 'page_id_top' => '4', + 'parent_id' => '1', + 'lft' => '2', + 'rght' => '7', + 'active' => true, + 'in_draft' => false, + 'default_role_key' => 'visitor', + 'need_approval' => true, + 'default_participation' => true, + 'page_layout_permitted' => true, + 'theme' => null, + ), + 'ParentPage' => array( + 'id' => '1', + 'room_id' => '2', + 'root_id' => null, + 'parent_id' => null, + 'lft' => '1', + 'rght' => '8', + 'permalink' => '', + 'slug' => null, + 'is_container_fluid' => false, + 'theme' => null, + ), + 'Space' => array( + 'id' => '2', + 'parent_id' => '1', + 'lft' => '2', + 'rght' => '3', + 'type' => '2', + 'plugin_key' => 'public_space', + 'default_setting_action' => 'rooms/index/2', + 'room_disk_size' => null, + 'room_id_root' => '2', + 'page_id_top' => '1', + 'permalink' => '', + 'is_m17n' => true, + 'after_user_save_model' => null, + ), + ); + + foreach ($removeKeys as $keyPath) { + $default = Hash::remove($default, $keyPath); + } + + $expected = Hash::merge($default, $mergeExpected); + + $this->assertEquals(Current::$current, $expected); + } + +} diff --git a/TestSuite/NetCommonsCurrentUtilityBase.php b/TestSuite/NetCommonsCurrentUtilityBase.php index a26ee816..70c202c5 100644 --- a/TestSuite/NetCommonsCurrentUtilityBase.php +++ b/TestSuite/NetCommonsCurrentUtilityBase.php @@ -36,6 +36,12 @@ class NetCommonsCurrentUtilityBase extends NetCommonsCakeTestCase { 'plugin.frames.frames_language4frames', 'plugin.frames.block4frames', 'plugin.frames.plugin4frames', + 'plugin.pages.box4pages', + 'plugin.pages.boxes_page_container4pages', + 'plugin.pages.page4pages', + 'plugin.pages.page_container4pages', + 'plugin.pages.roles_rooms_user4pages', + 'plugin.pages.room4pages', ); /** diff --git a/Utility/Current.php b/Utility/Current.php index d64396d0..639cfbee 100644 --- a/Utility/Current.php +++ b/Utility/Current.php @@ -292,6 +292,27 @@ class Current extends CurrentBase { */ protected static $_instance; +/** + * CurrentSystem Instance object + * + * @var mixed + */ + protected static $_instanceSystem; + +/** + * CurrentFrame Instance object + * + * @var mixed + */ + protected static $_instanceFrame; + +/** + * CurrentPage Instance object + * + * @var mixed + */ + protected static $_instancePage; + /** * setup current data * @@ -302,6 +323,15 @@ public static function initialize(Controller $controller) { if (! self::$_instance) { self::$_instance = new Current(); } + if (! self::$_instanceSystem) { + self::$_instanceSystem = new CurrentSystem(); + } + if (! self::$_instanceFrame) { + self::$_instanceFrame = new CurrentFrame(); + } + if (! self::$_instancePage) { + self::$_instancePage = new CurrentPage(); + } self::$request = clone $controller->request; self::$session = $controller->Session; @@ -326,14 +356,14 @@ public static function initialize(Controller $controller) { } self::$current['User'] = AuthComponent::user(); - (new CurrentSystem())->initialize(); + self::$_instanceSystem->initialize(); if (! self::isControlPanel()) { - (new CurrentFrame())->initialize(); + self::$_instanceFrame->initialize(); } //会員権限に紐づくパーミッションのセット - (new CurrentPage())->setDefaultRolePermissions(Hash::get(self::$current, 'User.role_key'), true); + self::$_instancePage->setDefaultRolePermissions(Hash::get(self::$current, 'User.role_key'), true); if (empty($controller->request->params['requested'])) { self::$originalCurrent = self::$current; diff --git a/Utility/CurrentFrame.php b/Utility/CurrentFrame.php index c0d269d8..3d8c4e83 100644 --- a/Utility/CurrentFrame.php +++ b/Utility/CurrentFrame.php @@ -11,6 +11,7 @@ App::uses('Container', 'Containers.Model'); App::uses('BlockSettingBehavior', 'Blocks.Model/Behavior'); +App::uses('CurrentPage', 'NetCommons.Utility'); /** * CurrentFrame Utility @@ -31,12 +32,23 @@ class CurrentFrame { Current::PLUGIN_WYSIWYG, ); +/** + * CurrentPage Instance object + * + * @var mixed + */ + protected static $_instancePage; + /** * setup current data * * @return void */ public function initialize() { + if (! self::$_instancePage) { + self::$_instancePage = new CurrentPage(); + } + $this->clear(); if (!in_array(Current::$request->params['plugin'], self::$skipFramePlugins, true)) { @@ -44,7 +56,7 @@ public function initialize() { $this->setBlock(); } - (new CurrentPage())->initialize(); + self::$_instancePage->initialize(); $this->setBlockRolePermissions(); } diff --git a/Utility/CurrentPage.php b/Utility/CurrentPage.php index b4219397..23c39c57 100644 --- a/Utility/CurrentPage.php +++ b/Utility/CurrentPage.php @@ -17,6 +17,7 @@ * * @author Shohei Nakajima * @package NetCommons\NetCommons\Utility + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ class CurrentPage { @@ -27,6 +28,13 @@ class CurrentPage { */ const DEFAULT_ROOM_ROLE_KEY = 'visitor'; +/** + * CurrentFrame Instance object + * + * @var mixed + */ + protected static $_instanceFrame; + /** * setup current data * @@ -156,28 +164,27 @@ private function __getPageConditions() { $pageId = Current::$request->data['Page']['id']; $conditions = array('Page.id' => $pageId); - } elseif (Current::$request->params['plugin'] === Current::PLUGIN_PAGES) { - if (Current::$request->params['controller'] === 'pages') { - $value = implode('/', Current::$request->params['pass']); - if ($value === '') { - $value = Page::PUBLIC_ROOT_PAGE_ID; - $conditions = array('Page.root_id' => $value); - } else { - $conditions = array( - 'Page.permalink' => $value, - 'Room.space_id' => Hash::get( - Current::$request->params, 'spaceId', Space::PUBLIC_SPACE_ID - ), - ); - } + } elseif (!empty(Current::$request->params['pageView'])) { + $value = implode('/', Current::$request->params['pass']); + if ($value === '') { + $value = Page::PUBLIC_ROOT_PAGE_ID; + $conditions = array('Page.root_id' => $value); } else { - $value = Hash::get(Current::$request->params, 'pass.1', ''); - if (! $value) { - $this->setRoom(Hash::get(Current::$request->params, 'pass.0', '')); - $value = Hash::get(Current::$current, 'Room.page_id_top', ''); - } - $conditions = array('Page.id' => $value); + $conditions = array( + 'Page.permalink' => $value, + 'Room.space_id' => Hash::get( + Current::$request->params, 'spaceId', Space::PUBLIC_SPACE_ID + ), + ); + } + + } elseif (!empty(Current::$request->params['pageEdit'])) { + $value = Hash::get(Current::$request->params, 'pass.1', ''); + if (! $value) { + $this->setRoom(Hash::get(Current::$request->params, 'pass.0', '')); + $value = Hash::get(Current::$current, 'Room.page_id_top', ''); } + $conditions = array('Page.id' => $value); } elseif (Hash::get(Current::$request->query, 'page_id')) { $pageId = Hash::get(Current::$request->query, 'page_id'); @@ -284,7 +291,10 @@ public function setPage() { Current::setCurrent($result); } - (new CurrentFrame())->setBoxPageContainer(); + if (! self::$_instanceFrame) { + self::$_instanceFrame = new CurrentFrame(); + } + self::$_instanceFrame->setBoxPageContainer(); } /**