Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

無駄な処理であるCurrent::$m17nを削除する #35

Merged
merged 1 commit into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions Model/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
* @copyright Copyright 2014, NetCommons Project
*/

App::uses('AppModel', 'Model');
App::uses('M17nAppModel', 'M17n.Model');

/**
* Language Model
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\M17n\Model
*/
class Language extends AppModel {
class Language extends M17nAppModel {

/**
* Validation rules
Expand All @@ -26,6 +26,13 @@ class Language extends AppModel {
*/
public $validate = array();

/**
* 言語データ
*
* @var array
*/
public static $languages = array();

/**
* Called during validation operations, before validation. Please note that custom
* validation rules can be defined in $validate.
Expand Down Expand Up @@ -61,13 +68,20 @@ public function beforeValidate($options = array()) {
}

/**
* 言語データタイプを取得
* 言語データを取得
*
* @param string $type 取得タイプ
* @param array $options 取得オプション
* @return array 言語データ
*/
public function getLanguage($type = 'all', $options = array()) {
public function getLanguage($type = null, $options = array()) {
if (! $type && ! $options) {
return $this->getLanguages();
}
if (! $type) {
$type = 'all';
}

if ($type === 'list' && ! isset($options['fields'])) {
$options['fields'] = array('id', 'code');
}
Expand All @@ -82,4 +96,25 @@ public function getLanguage($type = 'all', $options = array()) {
return $languages;
}

/**
* 言語データを取得
*
* @return array
*/
public function getLanguages() {
if (self::$languages) {
return self::$languages;
}

self::$languages = $this->find('all', array(
'recursive' => -1,
'conditions' => array(
'is_active' => true
),
'order' => array('weight' => 'asc')
));

return self::$languages;
}

}
9 changes: 9 additions & 0 deletions Model/M17nAppModel.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
<?php
/**
* M17n App Model
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/

App::uses('AppModel', 'Model');

/**
* M17n App Model
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\M17n\Model
*/
class M17nAppModel extends AppModel {

Expand Down
114 changes: 114 additions & 0 deletions Test/Case/Model/Language/GetLanguagesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/**
* Language::getLanguages()のテスト
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/

App::uses('M17nGetTest', 'M17n.TestSuite');
App::uses('Language', 'M17n.Model');

/**
* Language::getLanguages()のテスト
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\M17n\Test\Case\Model\Language
*/
class LanguageGetLanguagesTest extends M17nGetTest {

/**
* Fixtures
*
* @var array
*/
public $fixtures = array(
'plugin.m17n.language',
);

/**
* Plugin name
*
* @var string
*/
public $plugin = 'm17n';

/**
* Model name
*
* @var string
*/
protected $_modelName = 'Language';

/**
* Method name
*
* @var string
*/
protected $_methodName = 'getLanguages';

/**
* getLanguages()のテスト
*
* @return void
*/
public function testGetLanguages() {
$model = $this->_modelName;
$methodName = $this->_methodName;

//テスト実施
$result = $this->$model->$methodName();

//チェック
$expected = array(
0 => array(
'Language' => array(
'id' => '1',
'code' => 'en',
'weight' => '1',
'is_active' => true,
'created_user' => null,
'created' => '2014-07-03 05:00:39',
'modified_user' => null,
'modified' => '2014-07-03 05:00:39',
),
),
1 => array(
'Language' => array(
'id' => '2',
'code' => 'ja',
'weight' => '2',
'is_active' => true,
'created_user' => null,
'created' => '2014-07-03 05:00:39',
'modified_user' => null,
'modified' => '2014-07-03 05:00:39',
),
),
);
$this->assertEquals($expected, $result);
}

/**
* getLanguages()の既に取得済みの場合のテスト
*
* @return void
*/
public function testGetLanguagesWithData() {
$model = $this->_modelName;
$methodName = $this->_methodName;

Language::$languages = array('test dummy');

//テスト実施
$result = $this->$model->$methodName();

//チェック
$expected = array('test dummy');
$this->assertEquals($expected, $result);
}

}
77 changes: 77 additions & 0 deletions TestSuite/M17nGetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* M17nGetTest TestCase
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/

//@codeCoverageIgnoreStart;
App::uses('NetCommonsGetTest', 'NetCommons.TestSuite');
//@codeCoverageIgnoreEnd;

/**
* M17nGetTest TestCase
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\M17n\TestSuite
* @codeCoverageIgnore
*/
class M17nGetTest extends NetCommonsGetTest {

/**
* Fixtures
*
* @var array
*/
private $__fixtures = array();

/**
* Plugin name
*
* @var string
*/
public $plugin = 'm17n';

/**
* Fixtures load
*
* @param string $name The name parameter on PHPUnit_Framework_TestCase::__construct()
* @param array $data The data parameter on PHPUnit_Framework_TestCase::__construct()
* @param string $dataName The dataName parameter on PHPUnit_Framework_TestCase::__construct()
* @return void
*/
public function __construct($name = null, array $data = array(), $dataName = '') {
if (! isset($this->fixtures)) {
$this->fixtures = array();
}
$this->fixtures = array_merge($this->__fixtures, $this->fixtures);
parent::__construct($name, $data, $dataName);
}

/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();

Language::$languages = array();
}

/**
* tearDown method
*
* @return void
*/
public function tearDown() {
Language::$languages = array();

parent::tearDown();
}

}