From dca82ef529ab7542964cd441723184acc8251f0a Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Wed, 16 Nov 2016 23:43:47 +0900 Subject: [PATCH 1/2] =?UTF-8?q?UserImport=E3=83=86=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=81=A7=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=AB=E3=81=AA=E3=81=A3?= =?UTF-8?q?=E3=81=9F=E3=81=9F=E3=82=81=E6=94=B9=E4=BF=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Model/Behavior/OriginalKeyBehavior.php | 17 ++++++++++++++++- TestSuite/NetCommonsCakeTestCase.php | 3 +++ TestSuite/NetCommonsControllerBaseTestCase.php | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Model/Behavior/OriginalKeyBehavior.php b/Model/Behavior/OriginalKeyBehavior.php index f4b7547b..e3633ecb 100644 --- a/Model/Behavior/OriginalKeyBehavior.php +++ b/Model/Behavior/OriginalKeyBehavior.php @@ -29,6 +29,13 @@ class OriginalKeyBehavior extends ModelBehavior { 'priority' => 8 ); +/** + * PHPUnitで使用するキー配列 + * + * @var mixed + */ + public static $unitTestKeys; + /** * beforeSave is called before a model is saved. Returning false from a beforeSave callback * will abort the save operation. @@ -90,7 +97,15 @@ public static function generateKey($plugin, $dataSource) { if ($dataSource !== 'test') { return Security::hash($plugin . mt_rand() . microtime(), 'md5'); } else { - return Security::hash($plugin, 'md5'); + if (! self::$unitTestKeys) { + self::$unitTestKeys = array(); + } + if (! isset(self::$unitTestKeys[$plugin])) { + self::$unitTestKeys[$plugin] = Security::hash($plugin, 'md5'); + return self::$unitTestKeys[$plugin]; + } else { + return Security::hash($plugin . mt_rand() . microtime(), 'md5'); + } } } diff --git a/TestSuite/NetCommonsCakeTestCase.php b/TestSuite/NetCommonsCakeTestCase.php index aff19820..571ee968 100644 --- a/TestSuite/NetCommonsCakeTestCase.php +++ b/TestSuite/NetCommonsCakeTestCase.php @@ -15,6 +15,7 @@ App::uses('Current', 'NetCommons.Utility'); App::uses('NetCommonsTestSuite', 'NetCommons.TestSuite'); App::uses('SiteSettingUtil', 'SiteManager.Utility'); +App::uses('OriginalKeyBehavior', 'NetCommons.Model/Behavior'); /** * NetCommonsCakeTestCase class @@ -151,6 +152,8 @@ public function tearDown() { Current::$current = array(); Current::$permission = array(); + OriginalKeyBehavior::$unitTestKeys = array(); + parent::tearDown(); } diff --git a/TestSuite/NetCommonsControllerBaseTestCase.php b/TestSuite/NetCommonsControllerBaseTestCase.php index b6073e99..5556b021 100644 --- a/TestSuite/NetCommonsControllerBaseTestCase.php +++ b/TestSuite/NetCommonsControllerBaseTestCase.php @@ -20,6 +20,7 @@ App::uses('NetCommonsCakeTestCase', 'NetCommons.TestSuite'); App::uses('Role', 'Roles.Model'); App::uses('SiteSettingUtil', 'SiteManager.Utility'); +App::uses('OriginalKeyBehavior', 'NetCommons.Model/Behavior'); /** * NetCommonsControllerTestCase @@ -160,6 +161,8 @@ public function tearDown() { Current::$current = array(); Current::$permission = array(); + OriginalKeyBehavior::$unitTestKeys = array(); + SiteSettingUtil::reset(); parent::tearDown(); } From 38349fc41bc16b2ae6c172795c3b15203d3cac93 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Thu, 17 Nov 2016 10:46:37 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E4=B8=8B=E8=A8=98=E3=81=AE=E3=82=B3?= =?UTF-8?q?=E3=83=9F=E3=83=83=E3=83=88=E3=81=AB=E3=82=88=E3=82=8A=E3=80=81?= =?UTF-8?q?=E5=85=A8=E4=BD=93=E7=9A=84=E3=81=ABUnit=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=81=A7=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=AB=E3=81=AA?= =?UTF-8?q?=E3=82=8B=E3=81=9F=E3=82=81=E4=BF=AE=E6=AD=A3=20https://github.?= =?UTF-8?q?com/s-nakajima/NetCommons/commit/dca82ef529ab7542964cd441723184?= =?UTF-8?q?acc8251f0a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Model/Behavior/OriginalKeyBehavior.php | 14 +++----------- TestSuite/NetCommonsCakeTestCase.php | 2 +- TestSuite/NetCommonsControllerBaseTestCase.php | 2 +- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/Model/Behavior/OriginalKeyBehavior.php b/Model/Behavior/OriginalKeyBehavior.php index e3633ecb..807eca7e 100644 --- a/Model/Behavior/OriginalKeyBehavior.php +++ b/Model/Behavior/OriginalKeyBehavior.php @@ -34,7 +34,7 @@ class OriginalKeyBehavior extends ModelBehavior { * * @var mixed */ - public static $unitTestKeys; + public static $isUnitRandomKey = false; /** * beforeSave is called before a model is saved. Returning false from a beforeSave callback @@ -94,18 +94,10 @@ public function afterSave(Model $model, $created, $options = array()) { * @return string Hash key */ public static function generateKey($plugin, $dataSource) { - if ($dataSource !== 'test') { + if ($dataSource !== 'test' || self::$isUnitRandomKey) { return Security::hash($plugin . mt_rand() . microtime(), 'md5'); } else { - if (! self::$unitTestKeys) { - self::$unitTestKeys = array(); - } - if (! isset(self::$unitTestKeys[$plugin])) { - self::$unitTestKeys[$plugin] = Security::hash($plugin, 'md5'); - return self::$unitTestKeys[$plugin]; - } else { - return Security::hash($plugin . mt_rand() . microtime(), 'md5'); - } + return Security::hash($plugin, 'md5'); } } diff --git a/TestSuite/NetCommonsCakeTestCase.php b/TestSuite/NetCommonsCakeTestCase.php index 571ee968..f81da42c 100644 --- a/TestSuite/NetCommonsCakeTestCase.php +++ b/TestSuite/NetCommonsCakeTestCase.php @@ -152,7 +152,7 @@ public function tearDown() { Current::$current = array(); Current::$permission = array(); - OriginalKeyBehavior::$unitTestKeys = array(); + OriginalKeyBehavior::$isUnitRandomKey = false; parent::tearDown(); } diff --git a/TestSuite/NetCommonsControllerBaseTestCase.php b/TestSuite/NetCommonsControllerBaseTestCase.php index 5556b021..159d8bb1 100644 --- a/TestSuite/NetCommonsControllerBaseTestCase.php +++ b/TestSuite/NetCommonsControllerBaseTestCase.php @@ -161,7 +161,7 @@ public function tearDown() { Current::$current = array(); Current::$permission = array(); - OriginalKeyBehavior::$unitTestKeys = array(); + OriginalKeyBehavior::$isUnitRandomKey = false; SiteSettingUtil::reset(); parent::tearDown();