├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── config ├── Migrations │ └── 20150126111319_settings_initial.php ├── bootstrap.php └── routes.php ├── phpunit.xml.dist ├── src ├── Core │ └── Setting.php └── Model │ ├── Entity │ └── Configuration.php │ └── Table │ └── ConfigurationsTable.php ├── tests ├── Fixture │ └── SettingsConfigurationsFixture.php ├── TestCase │ ├── Core │ │ └── SettingTest.php │ └── Model │ │ └── Table │ │ └── ConfigurationsTableTest.php └── bootstrap.php └── webroot └── empty /.gitignore: -------------------------------------------------------------------------------- 1 | tmp/* 2 | [Cc]onfig/core.php 3 | [Cc]onfig/database.php 4 | app/tmp/* 5 | app/[Cc]onfig/core.php 6 | app/[Cc]onfig/database.php 7 | !empty 8 | vendor/ 9 | composer.lock 10 | error.log 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | 8 | sudo: false 9 | 10 | env: 11 | matrix: 12 | - DB=mysql db_dsn='mysql://travis@0.0.0.0/cakephp_test' 13 | - DB=pgsql db_dsn='postgres://travis@127.0.0.1/cakephp_test' 14 | - DB=sqlite db_dsn='sqlite:///:memory:' 15 | 16 | global: 17 | - DEFAULT=1 18 | 19 | matrix: 20 | fast_finish: true 21 | 22 | include: 23 | - php: 5.4 24 | env: PHPCS=1 DEFAULT=0 25 | 26 | - php: 5.4 27 | env: COVERALLS=1 DEFAULT=0 DB=mysql db_dsn='mysql://travis@0.0.0.0/cakephp_test' 28 | 29 | install: 30 | - composer self-update 31 | - composer install --prefer-dist --no-interaction --dev 32 | 33 | before_script: 34 | - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi" 35 | - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi" 36 | - sh -c "if [ '$PHPCS' = '1' ]; then composer require cakephp/cakephp-codesniffer:dev-master; fi" 37 | - sh -c "if [ '$COVERALLS' = '1' ]; then composer require --dev satooshi/php-coveralls:dev-master; fi" 38 | - sh -c "if [ '$COVERALLS' = '1' ]; then mkdir -p build/logs; fi" 39 | 40 | - phpenv rehash 41 | - set +H 42 | 43 | script: 44 | - sh -c "if [ '$DEFAULT' = '1' ]; then phpunit --stderr; fi" 45 | - sh -c "if [ '$PHPCS' = '1' ]; then ./vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi" 46 | - sh -c "if [ '$COVERALLS' = '1' ]; then phpunit --stderr --coverage-clover build/logs/clover.xml; fi" 47 | - sh -c "if [ '$COVERALLS' = '1' ]; then php vendor/bin/coveralls -c .coveralls.yml -v; fi" 48 | 49 | notifications: 50 | email: false -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 CakeManager 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Settings plugin for CakePHP 2 | 3 | [![Build Status](https://travis-ci.org/cakemanager/cakephp-settings.svg?branch=master)](https://travis-ci.org/cakemanager/cakephp-settings) 4 | [![Coverage Status](https://coveralls.io/repos/cakemanager/cakephp-settings/badge.svg?branch=master&service=github)](https://coveralls.io/github/cakemanager/cakephp-settings?branch=master) 5 | [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/cakemanager/cakephp-settings?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) 6 | 7 | The Settings Plugin allows you to manage your settings (normally used with cake's `Configure`-class) in your database. 8 | 9 | - Is easy to use: you can use the `Configure::read()` and `Configure::write()` methods via the [`Setting`-class](#using-the-class). 10 | 11 | - Also, you are able to read and write settings by your [console](#using-the-shell). 12 | 13 | - Last but not least: If you use the [CakeAdmin Plugin](https://github.com/cakemanager/cakephp-cakeadmin) you get an [automatically generated form](#using-the-settings-form) :). 14 | 15 | > Note: The Settings-plugin is prefix-minded. An example: `Prefix.Name`. 16 | 17 | ## Installation 18 | 19 | You can install this plugin into your CakePHP application using [composer](http://getcomposer.org). For existing applications you can add the following to your `composer.json` file: 20 | 21 | ```javascript 22 | "require": { 23 | "cakemanager/cakephp-settings": "dev-master" 24 | } 25 | ``` 26 | 27 | And run `/composer update`. 28 | 29 | ## Configuration 30 | 31 | You will need to add the following line to your application's bootstrap.php file: 32 | 33 | ```php 34 | Plugin::load('Settings', ['bootstrap' => true, 'routes' => true]); 35 | 36 | // or run in your shell 37 | 38 | $ bin/cake plugin load -b -r Settings 39 | ``` 40 | 41 | Next you need to create the table. Use the following command to initialize the settings-table. 42 | 43 | ``` 44 | $ bin/cake migrations migrate -p Settings 45 | ``` 46 | 47 | ## Usage 48 | 49 | The `Setting`-class works the same like the `Configure`-class from CakePHP itself. 50 | 51 | You can include the class with: 52 | 53 | ```php 54 | use Settings\Core\Setting; 55 | ``` 56 | 57 | ### Write 58 | 59 | You can write settings with the following: 60 | 61 | ```php 62 | Setting::write('App.Name', 'Custom Name'); 63 | ``` 64 | 65 | The value `Custom Name` is now written to the database with the key `App.Name`. The empty array can contain multiple options 66 | 67 | ### Read 68 | 69 | Now we gonna read the value from our just created key. Use: 70 | 71 | ```php 72 | Setting::read('App.Name'); 73 | ``` 74 | 75 | This will return our value: `Custom Name`. 76 | 77 | 78 | ### Register 79 | 80 | To prevent missing configurations when migrating to another environment the `register` method is introduced. 81 | Use the following to make sure the configuration exists in your application: 82 | 83 | ```php 84 | Setting::register('App.Name', 'Default Value', []); 85 | ``` 86 | 87 | #### Options 88 | The following options are available: 89 | - `description` - Description of your setting. 90 | - `type` - Type to use like `text`, `select`, and more. 91 | - `options` - Array with available options. In combination with the `type = select` option, this will generate a 92 | select-box with the given options. 93 | - `editable` - Bool if the setting should be editable. 94 | - `weight` - Weight (order) of the setting. 95 | 96 | The options key can handle multiple types. You can define an array with options, but you can also create a close to 97 | prevent long queries on every request. Example: 98 | 99 | ```php 100 | Setting::register('App.Index', false, [ 101 | 'options' => function() { 102 | return TableRegistry::get('Blogs')->find('list')->toArray(); 103 | } 104 | ]); 105 | ``` 106 | 107 | ## Using the setting-forms 108 | 109 | If you are using the [CakeAdmin Plugin](https://github.com/cakemanager/cakephp-cakeadmin), we will create a default form where you can edit your settings (if the field `editable` isset to `1`). The Settings-Plugin will automatically add a menu-item to the admin-area. 110 | 111 | If you click the menu-item you will see a list with all editable settings who contains the chosen prefix (or default: `App`). 112 | 113 | ### Register 114 | 115 | To add your prefix to the settings-list use the following: 116 | 117 | ```php 118 | Configure::write('Settings.Prefixes.Test', 'Test'); 119 | ``` 120 | 121 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cakemanager/cakephp-settings", 3 | "description": "Settings plugin for CakePHP", 4 | "type": "cakephp-plugin", 5 | "keywords": ["cakephp", "orm", "settings", "configure"], 6 | "require": { 7 | "php": ">=5.4", 8 | "cakephp/plugin-installer": "*", 9 | "cakephp/orm": "3.*", 10 | "cakemanager/cakephp-utils": "dev-master" 11 | }, 12 | "require-dev": { 13 | "cakephp/orm": "~3.0", 14 | "phpunit/phpunit": " 4.6.*@dev" 15 | }, 16 | "extra": { 17 | "installer-name": "Settings" 18 | }, 19 | "autoload": { 20 | "psr-4": { 21 | "Settings\\": "src" 22 | } 23 | }, 24 | "autoload-dev": { 25 | "psr-4": { 26 | "Settings\\Test\\": "tests", 27 | "Cake\\Test\\": "./vendor/cakephp/cakephp/tests", 28 | "App\\": "tests/App" 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /config/Migrations/20150126111319_settings_initial.php: -------------------------------------------------------------------------------- 1 | table('settings_configurations'); 27 | $table 28 | ->addColumn('name', 'string', [ 29 | 'limit' => '100', 30 | 'default' => '', 31 | ]) 32 | ->addColumn('value', 'text', [ 33 | 'null' => true 34 | ]) 35 | ->addColumn('description', 'text', [ 36 | 'null' => true 37 | ]) 38 | ->addColumn('type', 'string', [ 39 | 'limit' => '50', 40 | 'default' => '', 41 | ]) 42 | ->addColumn('editable', 'boolean', [ 43 | 'limit' => '1', 44 | 'default' => true, 45 | ]) 46 | ->addColumn('weight', 'integer', [ 47 | 'limit' => '11', 48 | 'default' => '0', 49 | ]) 50 | ->addColumn('autoload', 'boolean', [ 51 | 'limit' => '1', 52 | 'default' => true, 53 | ]) 54 | ->addColumn('created', 'datetime') 55 | ->addColumn('modified', 'datetime') 56 | ->create(); 57 | } 58 | 59 | /** 60 | * Migrate Down. 61 | * 62 | * @return void 63 | */ 64 | public function down() { 65 | 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /config/bootstrap.php: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | ./tests/TestCase 18 | 19 | 20 | 21 | 22 | 23 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | ./vendor/ 36 | ./vendor/ 37 | 38 | ./tests/ 39 | ./tests/ 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/Core/Setting.php: -------------------------------------------------------------------------------- 1 | findByName($key)->select('value'); 84 | 85 | if ($data->count() > 0) { 86 | $data = $data->first()->toArray(); 87 | } else { 88 | return null; 89 | } 90 | 91 | self::_store($key, $data['value']); 92 | 93 | $value = $data['value']; 94 | 95 | if ($type) { 96 | settype($value, $type); 97 | } 98 | 99 | return $value; 100 | } 101 | 102 | /** 103 | * write 104 | * 105 | * Method to write data to database. 106 | * 107 | * ### Example 108 | * 109 | * Setting::write('Plugin.Autoload', true); 110 | * 111 | * ### Options 112 | * - editable value if the setting is editable in the admin-area. Default 1 (so, editable) 113 | * - overrule boolean if the setting should be written if it already exists. Default true. 114 | * 115 | * Example: 116 | * Setting::write('Plugin.Autoload', false, [ 117 | * 'overrule' => true, 118 | * 'editable' => 0, 119 | * ] 120 | * 121 | * @param string $key Key of the value. Must contain an prefix. 122 | * @param mixed $value The value of the key. 123 | * @param array $options Options array. 124 | * @return void|bool 125 | */ 126 | public static function write($key, $value = null, $options = []) 127 | { 128 | if (!self::_tableExists()) { 129 | return; 130 | } 131 | 132 | self::autoLoad(); 133 | 134 | $_options = [ 135 | 'editable' => 1, 136 | 'overrule' => true, 137 | ]; 138 | 139 | $options = Hash::merge($_options, $options); 140 | 141 | $model = self::model(); 142 | 143 | if (self::check($key)) { 144 | if ($options['overrule']) { 145 | $data = $model->findByName($key)->first(); 146 | if ($data) { 147 | $data->set('value', $value); 148 | $model->save($data); 149 | } else { 150 | return false; 151 | } 152 | } else { 153 | return false; 154 | } 155 | } else { 156 | $data = $model->newEntity($options); 157 | $data->name = $key; 158 | $data->value = $value; 159 | $model->save($data); 160 | } 161 | 162 | self::_store($key, $value); 163 | 164 | return true; 165 | } 166 | 167 | /** 168 | * check 169 | * 170 | * Checks if an specific key exists. 171 | * Returns boolean. 172 | * 173 | * @param string $key Key. 174 | * @return bool|void 175 | */ 176 | public static function check($key) 177 | { 178 | if (!self::_tableExists()) { 179 | return; 180 | } 181 | 182 | self::autoLoad(); 183 | $model = self::model(); 184 | 185 | if (key_exists($key, self::$_data)) { 186 | return true; 187 | } 188 | 189 | $query = $model->findByName($key); 190 | 191 | if (!$query->Count()) { 192 | return false; 193 | } 194 | 195 | return true; 196 | } 197 | 198 | /** 199 | * model 200 | * 201 | * Returns an instance of the Configurations-model (Table). 202 | * Also used as setter for the instance of the model. 203 | * 204 | * @param \Cake\ORM\Table|null $model Model to use. 205 | * @return \Cake\ORM\Table 206 | */ 207 | public static function model($model = null) 208 | { 209 | if ($model) { 210 | self::$_model = $model; 211 | } 212 | 213 | if (!self::$_model) { 214 | self::$_model = TableRegistry::get('Settings.Configurations'); 215 | } 216 | 217 | return self::$_model; 218 | } 219 | 220 | /** 221 | * register 222 | * 223 | * Registers a setting and its default values. 224 | * 225 | * @param string $key The key. 226 | * @param mixed $value The default value. 227 | * @param array $data Custom data. 228 | * @return void 229 | */ 230 | public static function register($key, $value, $data = []) 231 | { 232 | if (!self::_tableExists()) { 233 | return; 234 | } 235 | 236 | self::autoLoad(); 237 | 238 | $_data = [ 239 | 'value' => $value, 240 | 'editable' => 1, 241 | 'autoload' => true, 242 | 'options' => [], 243 | 'description' => null, 244 | ]; 245 | 246 | $data = array_merge($_data, $data); 247 | 248 | // Don't overrule because we register 249 | $data['overrule'] = false; 250 | 251 | self::options($key, $data['options']); 252 | 253 | self::write($key, $data['value'], $data); 254 | } 255 | 256 | /** 257 | * options 258 | * 259 | * @param string $key Key for options. 260 | * @param array $value Options to use. 261 | * @return mixed 262 | */ 263 | public static function options($key, $value = null) 264 | { 265 | if (!self::_tableExists()) { 266 | return; 267 | } 268 | 269 | if ($value) { 270 | self::$_options[$key] = $value; 271 | } 272 | 273 | if (array_key_exists($key, self::$_options)) { 274 | return self::$_options[$key]; 275 | } else { 276 | return false; 277 | } 278 | } 279 | 280 | /** 281 | * autoLoad 282 | * 283 | * AutoLoad method. 284 | * Loads all configurations who are autoloaded. 285 | * 286 | * @return void 287 | */ 288 | public static function autoLoad() 289 | { 290 | if (!self::_tableExists()) { 291 | return; 292 | } 293 | if (self::$_autoloaded) { 294 | return; 295 | } 296 | self::$_autoloaded = true; 297 | 298 | $model = self::model(); 299 | 300 | $query = $model->find('all')->where(['autoload' => 1])->select(['name', 'value']); 301 | 302 | foreach ($query as $configure) { 303 | self::_store($configure->get('name'), $configure->get('value')); 304 | } 305 | } 306 | 307 | /** 308 | * clear 309 | * 310 | * Clears all settings out of the class. Settings 311 | * won't be deleted from database. 312 | * 313 | * @param bool $reload Bool if settings should be reloaded 314 | * @return void 315 | */ 316 | public static function clear($reload = false) 317 | { 318 | self::$_autoloaded = !$reload; 319 | self::$_data = []; 320 | } 321 | 322 | /** 323 | * _store 324 | * 325 | * Stores recent data in the $_data-variable. 326 | * 327 | * @param string $key The key. 328 | * @param mixed $value The value. 329 | * @return void 330 | */ 331 | protected static function _store($key, $value) 332 | { 333 | self::$_data[$key] = $value; 334 | } 335 | 336 | /** 337 | * _tableExists 338 | * 339 | * @return bool 340 | */ 341 | protected static function _tableExists() 342 | { 343 | $db = ConnectionManager::get('default'); 344 | $tables = $db->schemaCollection()->listTables(); 345 | 346 | if (in_array('settings_configurations', $tables)) { 347 | return true; 348 | } 349 | return false; 350 | } 351 | } 352 | -------------------------------------------------------------------------------- /src/Model/Entity/Configuration.php: -------------------------------------------------------------------------------- 1 | true, 32 | 'name' => true, 33 | 'value' => true, 34 | 'description' => true, 35 | 'type' => true, 36 | 'editable' => true, 37 | 'weight' => true, 38 | 'autoload' => true, 39 | ]; 40 | 41 | /** 42 | * _setKey 43 | * 44 | * Setter for the key. 45 | * 46 | * @param string $key The value. 47 | * @return void 48 | */ 49 | protected function _setKey($key) 50 | { 51 | $this->set('name', $key); 52 | } 53 | 54 | /** 55 | * _getKey 56 | * 57 | * Getter for the key. 58 | * 59 | * @return string 60 | */ 61 | protected function _getKey() 62 | { 63 | return $this->get('name'); 64 | } 65 | 66 | /** 67 | * _getOptionsArray 68 | * 69 | * Getter for `options`. Array's are json-decoded. 70 | * 71 | * @return array 72 | */ 73 | protected function _getOptions() 74 | { 75 | if (array_key_exists('name', $this->_properties)) { 76 | $options = Setting::options($this->_properties['name']); 77 | if (is_callable($options)) { 78 | return $options(); 79 | } 80 | return $options; 81 | } 82 | return false; 83 | } 84 | 85 | protected $_virtual = ['options']; 86 | } 87 | -------------------------------------------------------------------------------- /src/Model/Table/ConfigurationsTable.php: -------------------------------------------------------------------------------- 1 | table('settings_configurations'); 34 | $this->displayField('id'); 35 | $this->primaryKey('id'); 36 | $this->addBehavior('Timestamp'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tests/Fixture/SettingsConfigurationsFixture.php: -------------------------------------------------------------------------------- 1 | [ 32 | 'type' => 'integer' 33 | ], 34 | 'name' => [ 35 | 'type' => 'string', 36 | ], 37 | 'value' => [ 38 | 'type' => 'text', 39 | ], 40 | 'description' => [ 41 | 'type' => 'text', 42 | ], 43 | 'type' => [ 44 | 'type' => 'string', 45 | ], 46 | 'editable' => [ 47 | 'type' => 'integer', 48 | ], 49 | 'weight' => [ 50 | 'type' => 'integer', 51 | ], 52 | 'autoload' => [ 53 | 'type' => 'integer', 54 | 'default' => '1' 55 | ], 56 | 'created' => [ 57 | 'type' => 'datetime', 58 | ], 59 | 'modified' => [ 60 | 'type' => 'datetime', 61 | ], 62 | '_constraints' => [ 63 | 'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []], 64 | ], 65 | '_options' => [ 66 | 'engine' => 'InnoDB', 'collation' => 'latin1_swedish_ci' 67 | ], 68 | ]; 69 | } 70 | -------------------------------------------------------------------------------- /tests/TestCase/Core/SettingTest.php: -------------------------------------------------------------------------------- 1 | 'plugin.settings.settings_configurations' 33 | ]; 34 | 35 | /** 36 | * setUp method 37 | * 38 | * @return void 39 | */ 40 | public function setUp() 41 | { 42 | parent::setUp(); 43 | 44 | $this->Settings = TableRegistry::get('Settings.Configurations'); 45 | 46 | Setting::model($this->Settings); 47 | } 48 | 49 | /** 50 | * tearDown method 51 | * 52 | * @return void 53 | */ 54 | public function tearDown() 55 | { 56 | unset($this->Settings); 57 | 58 | Setting::clear(); 59 | 60 | parent::tearDown(); 61 | } 62 | 63 | /** 64 | * Test model-method 65 | * 66 | * @return void 67 | */ 68 | public function testModel() 69 | { 70 | $model = Setting::model(); 71 | 72 | $this->assertEquals($this->Settings, $model); 73 | 74 | $this->assertEquals('settings_configurations', $model->table()); 75 | $this->assertEquals('Configurations', $model->alias()); 76 | 77 | $_model = "Test"; 78 | 79 | Setting::model($_model); 80 | 81 | $this->assertEquals($_model, "Test"); 82 | } 83 | 84 | /** 85 | * Test clear 86 | * 87 | * @return void 88 | */ 89 | public function testClear() 90 | { 91 | Setting::write('App.test1', 1); 92 | Setting::write('App.test2', 2); 93 | Setting::write('App.test3', 3); 94 | 95 | $data = [ 96 | 'App.test1' => 1, 97 | 'App.test2' => 2, 98 | 'App.test3' => 3, 99 | ]; 100 | 101 | $this->assertEquals($data, Setting::read()); 102 | 103 | $this->assertEquals(3, $this->Settings->find('all')->count()); 104 | 105 | Setting::clear(); 106 | 107 | $this->assertEquals([], Setting::read()); 108 | 109 | $this->assertEquals(3, $this->Settings->find('all')->count()); 110 | } 111 | 112 | /** 113 | * Test check-method 114 | */ 115 | public function testCheck() 116 | { 117 | $this->assertFalse(Setting::check('App.UniqueName')); 118 | 119 | Setting::write('App.UniqueName', 'Test'); 120 | 121 | $this->assertTrue(Setting::check('App.UniqueName')); 122 | } 123 | 124 | public function testRead() 125 | { 126 | $data = [ 127 | 'name' => 'App.UniqueReadvalue', 128 | 'value' => 'UniqueValue' 129 | ]; 130 | 131 | $this->Settings->save($this->Settings->newEntity($data)); 132 | 133 | $this->assertEquals('UniqueValue', Setting::read('App.UniqueReadvalue')); 134 | $this->assertTrue(Setting::read('App.UniqueReadvalue', 'bool')); 135 | $this->assertEquals(0, Setting::read('App.UniqueReadvalue', 'integer')); 136 | $this->assertEquals('UniqueValue', Setting::read('App.UniqueReadvalue', 'string')); 137 | 138 | Setting::write('App.UniqueReadvalue', false); 139 | $this->assertFalse(Setting::read('App.UniqueReadvalue')); 140 | $this->assertFalse(Setting::read('App.UniqueReadvalue', 'bool')); 141 | $this->assertEquals(0, Setting::read('App.UniqueReadvalue')); 142 | $this->assertEquals(0, Setting::read('App.UniqueReadvalue', 'integer')); 143 | $this->assertEquals('', Setting::read('App.UniqueReadvalue', 'string')); 144 | 145 | Setting::write('App.UniqueReadvalue', true); 146 | $this->assertTrue(Setting::read('App.UniqueReadvalue')); 147 | $this->assertTrue(Setting::read('App.UniqueReadvalue', 'bool')); 148 | $this->assertEquals(1, Setting::read('App.UniqueReadvalue')); 149 | $this->assertEquals(1, Setting::read('App.UniqueReadvalue', 'integer')); 150 | $this->assertEquals('1', Setting::read('App.UniqueReadvalue', 'string')); 151 | } 152 | 153 | /** 154 | * Test write-method 155 | * 156 | * @return void 157 | */ 158 | public function testWrite() 159 | { 160 | $count = $this->Settings->find('all')->count(); 161 | 162 | $this->assertEquals(0, $count); 163 | 164 | Setting::write('App.WriteSimple', 'SimpleValue'); 165 | 166 | $this->assertEquals(1, $this->Settings->find('all')->count()); 167 | $value = $this->Settings->get(1); 168 | $this->assertEquals('App.WriteSimple', $value->name); 169 | $this->assertEquals('App.WriteSimple', $value->key); 170 | $this->assertEquals('SimpleValue', $value->value); 171 | $this->assertEmpty($value->description); 172 | $this->assertEmpty($value->type); 173 | $this->assertEquals(1, $value->editable); 174 | $this->assertEmpty($value->options); 175 | $this->assertEquals(0, $value->weight); 176 | 177 | Setting::write('App.WriteAdvanced', 'AdvancedValue', [ 178 | 'description' => 'Short description', 179 | 'type' => 'text', 180 | 'editable' => true, 181 | 'options' => [ 182 | 1 => 'One', 183 | 2 => 'Two' 184 | ], 185 | 'weight' => 20, 186 | 'autoload' => true, 187 | ]); 188 | 189 | $this->assertEquals(2, $this->Settings->find('all')->count()); 190 | 191 | $value = $this->Settings->get(2); 192 | $this->assertEquals('App.WriteAdvanced', $value->name); 193 | $this->assertEquals('App.WriteAdvanced', $value->key); 194 | $this->assertEquals('AdvancedValue', $value->value); 195 | $this->assertEquals('Short description', $value->description); 196 | $this->assertEquals('text', $value->type); 197 | $this->assertEquals(1, $value->editable); 198 | $this->assertEquals(20, $value->weight); 199 | $this->assertEquals(1, $value->autoload); 200 | } 201 | 202 | /** 203 | * Test register-method 204 | * 205 | * @return void 206 | */ 207 | public function testRegister() 208 | { 209 | $count = $this->Settings->find('all')->count(); 210 | 211 | $this->assertEquals(0, $count); 212 | 213 | Setting::register('App.WriteSimple', 'SimpleValue'); 214 | 215 | $this->assertEquals(1, $this->Settings->find('all')->count()); 216 | $value = $this->Settings->get(1); 217 | $this->assertEquals('App.WriteSimple', $value->name); 218 | $this->assertEquals('App.WriteSimple', $value->key); 219 | $this->assertEquals('SimpleValue', $value->value); 220 | $this->assertEmpty($value->description); 221 | $this->assertEmpty($value->type); 222 | $this->assertEquals(1, $value->editable); 223 | $this->assertEmpty($value->options); 224 | $this->assertEquals(0, $value->weight); 225 | 226 | Setting::write('App.WriteSimple', 'SecondValue'); 227 | 228 | $this->assertEquals('SecondValue', Setting::read('App.WriteSimple')); 229 | $this->assertEquals('SecondValue', $this->Settings->get(1)->value); 230 | 231 | Setting::register('App.WriteSimple', 'SimpleValue'); 232 | 233 | $this->assertEquals(1, $this->Settings->find('all')->count()); 234 | $this->assertEquals('SecondValue', Setting::read('App.WriteSimple')); 235 | $this->assertEquals('SecondValue', $this->Settings->get(1)->value); 236 | 237 | Setting::register('App.WriteAdvanced', 'AdvancedValue', [ 238 | 'description' => 'Short description', 239 | 'type' => 'text', 240 | 'editable' => true, 241 | 'weight' => 20, 242 | 'autoload' => true, 243 | ]); 244 | 245 | $this->assertEquals(2, $this->Settings->find('all')->count()); 246 | 247 | $value = $this->Settings->get(2); 248 | $this->assertEquals('App.WriteAdvanced', $value->name); 249 | $this->assertEquals('App.WriteAdvanced', $value->key); 250 | $this->assertEquals('AdvancedValue', $value->value); 251 | $this->assertEquals('Short description', $value->description); 252 | $this->assertEquals('text', $value->type); 253 | $this->assertEquals(1, $value->editable); 254 | $this->assertEquals(20, $value->weight); 255 | $this->assertEquals(1, $value->autoload); 256 | } 257 | 258 | /** 259 | * Test options-method 260 | */ 261 | public function testOptions() 262 | { 263 | Setting::register('App.Key', 1, [ 264 | 'options' => [ 265 | 0 => 'One', 266 | 1 => 'Two', 267 | ] 268 | ]); 269 | 270 | $expected = [ 271 | 0 => 'One', 272 | 1 => 'Two', 273 | ]; 274 | 275 | $this->assertEquals($expected, Setting::options('App.Key')); 276 | 277 | Setting::options('App.Second', [ 278 | 0 => 'One', 279 | 1 => 'Two', 280 | ]); 281 | } 282 | 283 | /** 284 | * Test autoload-method 285 | * 286 | * @return void 287 | */ 288 | public function testAutoload() 289 | { 290 | Setting::write('App.Test1', 'Test1'); 291 | Setting::write('App.Test2', 'Test2'); 292 | 293 | Setting::clear(); 294 | 295 | $this->assertEmpty(Setting::read()); 296 | 297 | Setting::clear(true); 298 | Setting::autoLoad(); 299 | 300 | $_array = [ 301 | 'App.Test1' => 'Test1', 302 | 'App.Test2' => 'Test2', 303 | ]; 304 | 305 | $this->assertEquals($_array, Setting::read()); 306 | } 307 | } 308 | -------------------------------------------------------------------------------- /tests/TestCase/Model/Table/ConfigurationsTableTest.php: -------------------------------------------------------------------------------- 1 | 'plugin.settings.settings_configurations' 33 | ]; 34 | 35 | /** 36 | * setUp method 37 | * 38 | * @return void 39 | */ 40 | public function setUp() 41 | { 42 | parent::setUp(); 43 | $this->Settings = TableRegistry::get('Settings.Configurations'); 44 | } 45 | 46 | /** 47 | * tearDown method 48 | * 49 | * @return void 50 | */ 51 | public function tearDown() 52 | { 53 | unset($this->Settings); 54 | 55 | parent::tearDown(); 56 | } 57 | 58 | /** 59 | * test Initialize 60 | * 61 | * @return void 62 | */ 63 | public function testInitialize() 64 | { 65 | $this->assertEquals('settings_configurations', $this->Settings->table()); 66 | $this->assertEquals('id', $this->Settings->displayField()); 67 | $this->assertEquals('id', $this->Settings->primaryKey()); 68 | } 69 | 70 | /** 71 | * test Entity 72 | * 73 | * @return void 74 | */ 75 | public function testEntity() 76 | { 77 | $this->assertEquals(0, $this->Settings->find('all')->count()); 78 | 79 | $data = [ 80 | 'key' => 'App.Key', 81 | 'value' => 'Value', 82 | 'description' => 'Custom Description', 83 | 'type' => 'text', 84 | 'editable' => true, 85 | 'weight' => 10, 86 | 'autoload' => true 87 | ]; 88 | 89 | $this->Settings->save($this->Settings->newEntity($data)); 90 | 91 | $this->assertEquals(1, $this->Settings->find('all')->count()); 92 | 93 | $entity = $this->Settings->get(1); 94 | 95 | $this->assertEquals('App.Key', $entity->name); 96 | $this->assertEquals('App.Key', $entity->key); 97 | $this->assertEquals('Value', $entity->value); 98 | $this->assertEquals('Custom Description', $entity->description); 99 | $this->assertEquals('text', $entity->type); 100 | $this->assertEquals(1, $entity->editable); 101 | $this->assertEquals(10, $entity->weight); 102 | $this->assertEquals(1, $entity->autoload); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | register(); 47 | 48 | require_once CORE_PATH . 'config' . DS . 'bootstrap.php'; 49 | 50 | date_default_timezone_set('UTC'); 51 | mb_internal_encoding('UTF-8'); 52 | 53 | Configure::write('debug', true); 54 | Configure::write('App', [ 55 | 'namespace' => 'Settings\Test\App', 56 | 'encoding' => 'UTF-8', 57 | 'base' => false, 58 | 'baseUrl' => false, 59 | 'dir' => 'src', 60 | 'webroot' => 'webroot', 61 | 'www_root' => APP . 'webroot', 62 | 'fullBaseUrl' => 'http://localhost', 63 | 'imageBaseUrl' => 'img/', 64 | 'jsBaseUrl' => 'js/', 65 | 'cssBaseUrl' => 'css/', 66 | 'paths' => [ 67 | 'plugins' => [APP . 'Plugin' . DS], 68 | 'templates' => [APP . 'Template' . DS] 69 | ] 70 | ]); 71 | 72 | Cache::config([ 73 | '_cake_core_' => [ 74 | 'engine' => 'File', 75 | 'prefix' => 'cake_core_', 76 | 'serialize' => true 77 | ], 78 | '_cake_model_' => [ 79 | 'engine' => 'File', 80 | 'prefix' => 'cake_model_', 81 | 'serialize' => true 82 | ] 83 | ]); 84 | 85 | // Ensure default test connection is defined 86 | if (!getenv('db_class')) { 87 | putenv('db_class=Cake\Database\Driver\Sqlite'); 88 | putenv('db_dsn=sqlite::memory:'); 89 | } 90 | 91 | ConnectionManager::config('test', [ 92 | 'className' => 'Cake\Database\Connection', 93 | 'driver' => getenv('db_class'), 94 | 'dsn' => getenv('db_dsn'), 95 | 'database' => getenv('db_database'), 96 | 'username' => getenv('db_login'), 97 | 'password' => getenv('db_password'), 98 | 'timezone' => 'UTC' 99 | ]); 100 | 101 | Configure::write('Session', [ 102 | 'defaults' => 'php' 103 | ]); 104 | 105 | Log::config([ 106 | 'debug' => [ 107 | 'engine' => 'Cake\Log\Engine\FileLog', 108 | 'levels' => ['notice', 'info', 'debug'], 109 | 'file' => 'debug', 110 | ], 111 | 'error' => [ 112 | 'engine' => 'Cake\Log\Engine\FileLog', 113 | 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'], 114 | 'file' => 'error', 115 | ] 116 | ]); 117 | 118 | Plugin::load('Utils', ['path' => ROOT_UTILS]); 119 | Plugin::load('Settings', ['path' => ROOT, 'bootstrap' => true, 'routes' => true]); 120 | 121 | Carbon\Carbon::setTestNow(Carbon\Carbon::now()); 122 | 123 | DispatcherFactory::add('Routing'); 124 | DispatcherFactory::add('ControllerFactory'); 125 | -------------------------------------------------------------------------------- /webroot/empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cakemanager/cakephp-settings/4be834cdf4ee3502f43b85cad98e1b1eb4b24ced/webroot/empty --------------------------------------------------------------------------------