Commit 7d64e93c authored by disem's avatar disem

Merge branch 'master' of git://github.com/yiisoft/yii2 into dynamic-model-typo-fix

parents a83d4415 3a4b32c4
......@@ -21,6 +21,10 @@ services:
- elasticsearch
- mongodb
# try running against postgres 9.3
addons:
postgresql: "9.3"
install:
- composer self-update && composer --version
# core framework:
......
......@@ -113,7 +113,7 @@ it will upgrade your database to the last state according migrations.
To be able to run acceptance tests you need a running webserver. For this you can use the php builtin server and run it in the directory where your main project folder is located. For example if your application is located in `/www/advanced` all you need to is:
`cd /www` and then `php -S 127.0.0.1:8080` because the default configuration of acceptance tests expects the url of the application to be `/advanced/`.
If you already have a server configured or your application is not located in a folder called `advanced`, you may need to adjust the `TEST_ENTRY_URL` in `frontend/tests/_bootstrap.php` and `backend/tests/_bootstrap.php`.
If you already have a server configured or your application is not located in a folder called `advanced`, you may need to adjust the `test_entry_url` in `backend/codeception.yml` and `frontend/codeception.yml`.
After that is done you should be able to run your tests, for example to run `frontend` tests do:
......@@ -123,5 +123,7 @@ After that is done you should be able to run your tests, for example to run `fro
In similar way you can run tests for other application tiers - `backend`, `console`, `common`.
If you already have run `../vendor/bin/codecept build` for each application, you can run all tests by one command: `vendor/bin/codecept run`
You also can adjust you application suite configs and `_bootstrap.php` settings to use other urls and files, as it is can be done in `yii2-basic`.
Current template also includes [yii2-faker](https://github.com/yiisoft/yii2/tree/master/extensions/faker) extension, that is correctly setup for each application tier.
namespace: backend
actor: Tester
paths:
tests: tests
......@@ -17,3 +18,7 @@ modules:
user: ''
password: ''
dump: tests/_data/dump.sql
config:
# the entry script URL (without host info) for functional and acceptance tests
# PLEASE ADJUST IT TO THE ACTUAL ENTRY SCRIPT URL
test_entry_url: /advanced/backend/web/index-test.php
<?php
// the entry script URL (without host info) for functional and acceptance tests
// PLEASE ADJUST IT TO THE ACTUAL ENTRY SCRIPT URL
defined('TEST_ENTRY_URL') or define('TEST_ENTRY_URL', '/advanced/backend/web/index-test.php');
// the entry script file path for functional and acceptance tests
defined('TEST_ENTRY_FILE') or define('TEST_ENTRY_FILE', dirname(__DIR__) . '/web/index-test.php');
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
......@@ -18,6 +11,8 @@ require_once(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/aliases.php');
// set correct script paths
$_SERVER['SCRIPT_FILENAME'] = TEST_ENTRY_FILE;
$_SERVER['SCRIPT_NAME'] = TEST_ENTRY_URL;
// the entry script file path for functional and acceptance tests
$_SERVER['SCRIPT_FILENAME'] = dirname(__DIR__) . '/web/index-test.php';
$_SERVER['SCRIPT_NAME'] = \Codeception\Configuration::config()['config']['test_entry_url'];
$_SERVER['SERVER_NAME'] = 'localhost';
<?php
use common\tests\_pages\LoginPage;
use backend\WebGuy;
$I = new WebGuy($scenario);
$I->wantTo('ensure login page works');
......
<?php
use common\tests\_pages\LoginPage;
use backend\TestGuy;
$I = new TestGuy($scenario);
$I->wantTo('ensure login page works');
......
<?php
// set correct script paths
$_SERVER['SCRIPT_FILENAME'] = TEST_ENTRY_FILE;
$_SERVER['SCRIPT_NAME'] = TEST_ENTRY_URL;
$_SERVER['SCRIPT_FILENAME'] = dirname(dirname(__DIR__)) . '/web/index-test.php';
$_SERVER['SCRIPT_NAME'] = \Codeception\Configuration::config()['config']['test_entry_url'];;
return yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../../config/main.php'),
......
include:
- common
- console
- backend
- frontend
paths:
log: tests/_log
settings:
colors: true
namespace: common
actor: Tester
paths:
tests: tests
......
<?php
// the entry script URL (without host info) for functional and acceptance tests
// PLEASE ADJUST IT TO THE ACTUAL ENTRY SCRIPT URL
defined('TEST_ENTRY_URL') or define('TEST_ENTRY_URL', '/index-test.php');
// the entry script file path for functional and acceptance tests
defined('TEST_ENTRY_FILE') or define('TEST_ENTRY_FILE', dirname(__DIR__) . '/index-test.php');
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
......@@ -18,6 +11,4 @@ require_once(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/aliases.php');
// set correct script paths
$_SERVER['SCRIPT_FILENAME'] = TEST_ENTRY_FILE;
$_SERVER['SCRIPT_NAME'] = TEST_ENTRY_URL;
$_SERVER['SERVER_NAME'] = 'localhost';
namespace: console
actor: Tester
paths:
tests: tests
......
<?php
// the entry script URL (without host info) for functional and acceptance tests
// PLEASE ADJUST IT TO THE ACTUAL ENTRY SCRIPT URL
defined('TEST_ENTRY_URL') or define('TEST_ENTRY_URL', '/index-test.php');
// the entry script file path for functional and acceptance tests
defined('TEST_ENTRY_FILE') or define('TEST_ENTRY_FILE', dirname(__DIR__) . '/index-test.php');
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
......@@ -18,6 +11,4 @@ require_once(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/aliases.php');
// set correct script paths
$_SERVER['SCRIPT_FILENAME'] = TEST_ENTRY_FILE;
$_SERVER['SCRIPT_NAME'] = TEST_ENTRY_URL;
$_SERVER['SERVER_NAME'] = 'localhost';
namespace: frontend
actor: Tester
paths:
tests: tests
......@@ -17,3 +18,7 @@ modules:
user: ''
password: ''
dump: tests/_data/dump.sql
config:
# the entry script URL (without host info) for functional and acceptance tests
# PLEASE ADJUST IT TO THE ACTUAL ENTRY SCRIPT URL
test_entry_url: /advanced/frontend/web/index-test.php
<?php
// the entry script URL (without host info) for functional and acceptance tests
// PLEASE ADJUST IT TO THE ACTUAL ENTRY SCRIPT URL
defined('TEST_ENTRY_URL') or define('TEST_ENTRY_URL', '/advanced/frontend/web/index-test.php');
// the entry script file path for functional and acceptance tests
defined('TEST_ENTRY_FILE') or define('TEST_ENTRY_FILE', dirname(__DIR__) . '/web/index-test.php');
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
......@@ -18,6 +11,8 @@ require_once(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/aliases.php');
// set correct script paths
$_SERVER['SCRIPT_FILENAME'] = TEST_ENTRY_FILE;
$_SERVER['SCRIPT_NAME'] = TEST_ENTRY_URL;
// the entry script file path for functional and acceptance tests
$_SERVER['SCRIPT_FILENAME'] = dirname(__DIR__) . '/web/index-test.php';
$_SERVER['SCRIPT_NAME'] = \Codeception\Configuration::config()['config']['test_entry_url'];
$_SERVER['SERVER_NAME'] = 'localhost';
<?php
use frontend\tests\_pages\AboutPage;
use frontend\WebGuy;
$I = new WebGuy($scenario);
$I->wantTo('ensure that about works');
......
<?php
use frontend\tests\_pages\ContactPage;
use frontend\WebGuy;
$I = new WebGuy($scenario);
$I->wantTo('ensure that contact works');
......
<?php
use frontend\WebGuy;
$I = new WebGuy($scenario);
$I->wantTo('ensure that home page works');
$I->amOnPage(Yii::$app->homeUrl);
......
<?php
use common\tests\_pages\LoginPage;
use frontend\WebGuy;
$I = new WebGuy($scenario);
$I->wantTo('ensure login page works');
......
<?php
use frontend\tests\_pages\AboutPage;
use frontend\TestGuy;
$I = new TestGuy($scenario);
$I->wantTo('ensure that about works');
......
<?php
use frontend\tests\_pages\ContactPage;
use frontend\TestGuy;
$I = new TestGuy($scenario);
$I->wantTo('ensure that contact works');
......
<?php
use frontend\TestGuy;
$I = new TestGuy($scenario);
$I->wantTo('ensure that home page works');
$I->amOnPage(Yii::$app->homeUrl);
......
<?php
use common\tests\_pages\LoginPage;
use frontend\TestGuy;
$I = new TestGuy($scenario);
$I->wantTo('ensure login page works');
......
<?php
// set correct script paths
$_SERVER['SCRIPT_FILENAME'] = TEST_ENTRY_FILE;
$_SERVER['SCRIPT_NAME'] = TEST_ENTRY_URL;
$_SERVER['SCRIPT_FILENAME'] = dirname(dirname(__DIR__)) . '/web/index-test.php';
$_SERVER['SCRIPT_NAME'] = \Codeception\Configuration::config()['config']['test_entry_url'];;
return yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../../config/main.php'),
......
Аутентификация
==============
В отличие от Web-приложений, RESTful API обычно не сохраняют информацию о состоянии, а это означает, что сессии и куки
использовать не следует. Следовательно, раз состояние аутентификации пользователя не может быть сохранено в сессиях или куках,
каждый запрос должен приходить вместе с определенным видом параметров аутентификации. Общепринятая практика состоит в том,
что для аутентификации пользователя с каждый запросом отправляется секретный токен доступа. Так как токен доступа
может использоваться для уникальной идентификации и аутентификации пользователя, **запросы к API всегда должны отсылаться
через протокол HTTPS, чтобы предотвратить атаки «человек посередине» (англ. "man-in-the-middle", MitM)**.
Есть различные способы отправки токена доступа:
* [HTTP Basic Auth](http://en.wikipedia.org/wiki/Basic_access_authentication): токен доступа
отправляется как имя пользователя. Такой подход следует использовать только в том случае, когда токен доступа может быть безопасно сохранен
на стороне абонента API. Например, если API используется программой, запущенной на сервере.
* Параметр запроса: токен доступа отправляется как параметр запроса в URL-адресе API, т.е. примерно таким образом:
`https://example.com/users?access-token=xxxxxxxx`. Так как большинство Web-серверов сохраняют параметры запроса в своих логах,
такой подход следует применять только при работе с `JSONP`-запросами, которые не могут отправлять токены доступа
в HTTP-заголовках.
* [OAuth 2](http://oauth.net/2/): токен доступа выдается абоненту API сервером авторизации
и отправляется API-серверу через [HTTP Bearer Tokens](http://tools.ietf.org/html/rfc6750),
в соответствии с протоколом OAuth2.
Yii поддерживает все выше перечисленные методы аутентификации. Вы также можете легко создавать новые методы аутентификации.
Чтобы включить аутентификацию для ваших API, выполните следующие шаги:
1. У компонента приложения `user` установите свойство [[yii\web\User::enableSession|enableSession]] равным false.
2. Укажите, какие методы аутентификации вы планируете использовать, настроив поведение `authenticator`
в ваших классах REST-контроллеров.
3. Реализуйте метод [[yii\web\IdentityInterface::findIdentityByAccessToken()]] *в вашем [[yii\web\User::identityClass|классе UserIdentity]]*.
Шаг 1 не обязателен, но рекомендуется его все-таки выполнить, так как RESTful API не должны сохранять информацию о состоянии клиента. Когда свойство [[yii\web\User::enableSession|enableSession]]
установлено в false, состояние аутентификации пользователя НЕ БУДЕТ постоянно
сохраняться между запросами с использованием сессий. Вместо этого аутентификация будет выполняться для каждого запроса, что достигается шагами 2 и 3.
> Подсказка: если вы разрабатываете RESTful API в пределах приложения, вы можете настроить свойство
[[yii\web\User::enableSession|enableSession]] компонента приложения `user` в конфигурации приложения. Если вы разрабатываете
RESTful API как модуль, можете добавить следующую строчку в метод `init()` модуля:
> ```php
public function init()
{
parent::init();
\Yii::$app->user->enableSession = false;
}
```
Например, для использования HTTP Basic Auth, вы можете настроить свойство `authenticator` следующим образом:
```php
use yii\filters\auth\HttpBasicAuth;
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBasicAuth::className(),
];
return $behaviors;
}
```
Если вы хотите включить поддержку всех трех описанных выше методов аутентификации, можете использовать `CompositeAuth`:
```php
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBasicAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\auth\QueryParamAuth;
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => CompositeAuth::className(),
'authMethods' => [
HttpBasicAuth::className(),
HttpBearerAuth::className(),
QueryParamAuth::className(),
],
];
return $behaviors;
}
```
Каждый элемент в массиве `authMethods` должен быть названием класса метода аутентификации или массивом настроек.
Реализация метода `findIdentityByAccessToken()` определяется особенностями приложения. Например, в простом варианте,
когда у каждого пользователя есть только один токен доступа, вы можете хранить этот токен в поле `access_token`
таблицы пользователей. В этом случае метод `findIdentityByAccessToken()` может быть легко реализован в классе `User` следующим образом:
```php
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class User extends ActiveRecord implements IdentityInterface
{
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
}
```
После включения аутентификации описанным выше способом при каждом запросе к API запрашиваемый контроллер
будет пытаться аутентифицировать пользователя в своем методе `beforeAction()`.
Если аутентификация прошла успешно, контроллер выполнит другие проверки (ограничение на количество запросов, авторизация)
и затем выполнит действие. *Информация о подлинности аутентифицированного пользователя может быть получена из объекта `Yii::$app->user->identity`*.
Если аутентификация прошла неудачно, будет возвращен ответ с HTTP-кодом состояния 401 вместе с другими необходимыми заголовками
(такими, как заголовок `WWW-Authenticate` для HTTP Basic Auth).
## Авторизация <a name="authorization"></a>
После аутентификации пользователя вы, вероятно, захотите проверить, есть ли у него или у нее разрешение на выполнение запрошенного
действия с запрошенным ресурсом. Этот процесс называется *авторизацией* и подробно описан
в разделе [Авторизация](security-authorization.md).
Если ваши контроллеры унаследованы от [[yii\rest\ActiveController]], вы можете переопределить
метод [[yii\rest\Controller::checkAccess()|checkAccess()]] для выполнения авторизации. Этот метод будет вызываться
встроенными действиями, предоставляемыми контроллером [[yii\rest\ActiveController]].
This diff is collapsed.
Маршрутизация
=============
Имея готовые классы ресурсов и контроллеров, можно получить доступ к ресурсам, используя URL вроде
`http://localhost/index.php?r=user/create`, подобно тому, как вы это делаете с обычными Web-приложениями.
На деле вам обычно хочется включить «красивые» URL-адреса и использовать все преимущества HTTP-методов (HTTP-verbs).
Например, чтобы запрос `POST /users` означал обращение к действию `user/create`.
Это может быть легко сделано с помощью настройки компонента приложения `urlManager` в
конфигурации приложения следующим образом:
```php
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
],
]
```
Главная новинка в коде выше по сравнению с управлением URL-адресами в Web-приложениях состоит в использовании
[[yii\rest\UrlRule]] для маршрутизации запросов к RESTful API. Этот особый класс URL-правил будет
создавать целый набор дочерних URL-правил для поддержки маршрутизации и создания URL-адресов для указанного контроллера (или контроллеров).
Например, приведенный выше код является приближенным аналогом следующего набора правил:
```php
[
'PUT,PATCH users/<id>' => 'user/update',
'DELETE users/<id>' => 'user/delete',
'GET,HEAD users/<id>' => 'user/view',
'POST users' => 'user/create',
'GET,HEAD users' => 'user/index',
'users/<id>' => 'user/options',
'users' => 'user/options',
]
```
Этим правилом поддерживаются следующие точки входа в API:
* `GET /users`: разбитый на страницы список всех пользователей;
* `HEAD /users`: общая информация по списку пользователей;
* `POST /users`: создание нового пользователя;
* `GET /users/123`: подробная информация о пользователе 123;
* `HEAD /users/123`: общая информация о пользователе 123;
* `PATCH /users/123` и `PUT /users/123`: обновление пользователя 123;
* `DELETE /users/123`: удаление пользователя 123;
* `OPTIONS /users`: список HTTP-методов, поддерживаемые точкой входа `/users`;
* `OPTIONS /users/123`: список HTTP-методов, поддерживаемые точкой входа `/users/123`.
Вы можете настроить опции `only` и `except`, явно указав для них список действий, которые поддерживаются или
которые должны быть отключены, соответственно. Например:
```php
[
'class' => 'yii\rest\UrlRule',
'controller' => 'user',
'except' => ['delete', 'create', 'update'],
],
```
Вы также можете настроить опции `patterns` или `extraPatterns` для переопределения существующих шаблонов или добавления новых шаблонов, поддерживаемых этим правилом.
Например, для включения нового действия `search` в точке входа `GET /users/search` настройте опцию `extraPatterns` следующим образом:
```php
[
'class' => 'yii\rest\UrlRule',
'controller' => 'user',
'extraPatterns' => [
'GET search' => 'search',
],
```
Как вы могли заметить, ID контроллера `user` в этих точках входа используется в форме множественного числа (как `users`).
Это происходит потому, что [[yii\rest\UrlRule]] автоматически приводит идентификаторы контроллеров к множественной форме для использования в точках входа.
Вы можете отключить такое поведение, назначив свойству [[yii\rest\UrlRule::pluralize]] значение false, или, если вы хотите использовать
какие-то особые имена, вы можете настроить свойство [[yii\rest\UrlRule::controller]].
Генерация кода при помощи Gii
========================
В этом разделе, мы опишем как использовать [Gii](tool-gii.md) для автоматической генерации кода
реализующий некоторые общие особенности. Для достижения этой цели, все что вам нужно это просто ввести необходимую информацию в соответствии с инструкциями отображаемыми на веб-страницах Gii.
В этом руководстве, вы узнаете
* Как включить Gii в приложении;
* Как использовать Gii для создания Active Record класса;
* Как использовать Gii для генерироции кода реализующего CRUD для таблицы БД.
* Как настроить код генерируемый Gii.
Запускаем Gii <a name="starting-gii"></a>
------------
[Gii](tool-gii.md) поставляется в Yii как [модуль](structure-modules.md). Вы можете включить Gii
настроив его свойство [[yii\base\Application::modules|modules]] приложения. В частности, вы можете найти следующий код уже приведен в `config/web.php` файле - настройки приложения,
```php
$config = [ ... ];
if (YII_ENV_DEV) {
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = 'yii\gii\Module';
}
```
Приведенная выше конфигурация показывает, что когда в [среде разработки](concept-configurations.md#environment-constants),
приложение должно включать в себя модуль с именем `gii`, который реализует класс [[yii\gii\Module]].
Если вы проверите [входной скрипт](structure-entry-scripts.md) `web/index.php` вашего приложения, вы
найдете следующую строку, которая по сути делает `YII_ENV_DEV`, чтобы быть правдой.
```php
defined('YII_ENV') or define('YII_ENV', 'dev');
```
Благодаря этой строке, ваше приложение находится в режиме разработки, и Gii будет уже включен, в соответствии с описанной выше конфигурации. Теперь вы можете получить доступ к Gii по следующему адресу:
```
http://hostname/index.php?r=gii
```
![Gii](images/start-gii.png)
Генерация класса Active Record <a name="generating-ar"></a>
---------------------------------
Для использования Gii генератора класса Active Record, выберите "Генератор модели" (нажав на ссылку на главной странице Gii). И заполните форму следующим образом:
* Имя таблицы: `country`
* Класс модели : `Country`
![Генератор модели](images/start-gii-model.png)
Затем нажмите на кнопку "Предварительный просмотр". Вы увидите `models/Country.php` перечислен в результатах файл класса, который будет создан. Вы можете нажать на имя файла класса, для просмотра его содержимого.
При использовании Gii, если вы уже создали такой же файл и хотите перезаписать его, нажмите кнопку `различия` рядом с именем файла, чтобы увидеть различия между генерируемого кода и существующей версии.
![Предварительный просмотр генератора модели](images/start-gii-model-preview.png)
Если необхадимо перезаписать существующего файла, установите флажок рядом с "переписать", а затем нажмите кнопку "Создать". При создании нового файла, вы можете просто нажать "Создать".
Далее, вы увидите страницу подтверждения, указывающую что код был успешно создана. Если ваш файл уже существующествует, вы также увидите сообщение о том, что он был переписан с вновь сгенерированным кодом.
Создание CRUD кода <a name="generating-crud"></a>
--------------------
CRUD расшифровывается как Создать, Читать, Обновить, и Удалить, представляющих четыре общие задачи, принятые с данными на большинстве веб-сайтов. Чтобы создать функциональность CRUD используйте Gii, выберите "CRUD Генератор" (нажав на ссылку на главной странице GII). Для «country», например, заполнить полученную форму следующим образом:
* Класс модели : `app\models\Country`
* Класс модели поиска: `app\models\CountrySearch`
* Класс контроллера: `app\controllers\CountryController`
![CRUD генератор](images/start-gii-crud.png)
Затем, нажмите на кнопку "Предварительный просмотр". Вы увидите список файлов, которые будут созданы, как показано ниже.
[[ЗДЕСЬ НУЖНА КАРТИНКА]]
Если вы ранее создали `controllers/CountryController.php` и `views/country/index.php` файлы (в разделе базы данных направляющей), установите флажок "переписать", чтобы заменить их. (Предыдущие версии не имеют полной поддержку CRUD.)
Пытается его <a name="trying-it-out"></a>
-------------
Чтобы увидеть как она работает, используйте ваш браузер для доступа к следующему URL:
```
http://hostname/index.php?r=country/index
```
Вы увидите таблицу данных показывающией страны из таблицы базы данных. Вы можете сортировать, или фильтр необходимо ввести условия фильтрации в заголовки столбцов.
Для каждой страны отображающейся в таблице, вы можете просмотреть подробную информацию, обновить ее или удалить.
Вы также можете нажать на кнопку "Создать страну" в верхней части таблицы и будет предоставлена форма для создания новой страны.
![Таблица данных стран](images/start-gii-country-grid.png)
![Обновление страны](images/start-gii-country-update.png)
Ниже приведен список файлов созданных с помощью Gii, в случае если вы хотите исследовать эту реализацию функций, или их настройки:
* Контроллер: `controllers/CountryController.php`
* Модели: `models/Country.php` and `models/CountrySearch.php`
* Вид: `views/country/*.php`
> Информация: Gii разработан как высоко настраиваемый и расширяемый инструмент генерации кода. Используя его с умом может значительно ускорить скорость разработки приложений. Для более подробной информации, пожалуйста обратитесь к разделу [Gii](tool-gii.md).
Заключение <a name="summary"></a>
-------
В этом разделе, Вы узнали как использовать Gii чтобы генерировать код который реализует полный функциональность CRUD для содержимого которое хранится в таблице базы данных.
\ No newline at end of file
......@@ -20,7 +20,7 @@ $customer->save();
```
The above code is equivalent to using the following raw SQL statement, which is less
intuitive, more error prone, and may have compatibility problem for different DBMS:
intuitive, more error prone, and may have compatibility problems for different DBMS:
```php
$db->createCommand('INSERT INTO customer (name) VALUES (:name)', [
......
......@@ -140,7 +140,52 @@ You may specify various container HTML options passing arrays to:
Data column is for displaying and sorting data. It is default column type so specifying class could be omitted when
using it.
TBD
The main setting of the data column is its format. It could be specified via `format` attribute. Its values are
corresponding to methods in `format` application component that is [[\yii\base\Formatter|Formatter]] by default:
```php
<?= GridView::widget([
'columns' => [
[
'attribute' => 'name',
'format' => 'text'
],
[
'attribute' => 'birthday',
'format' => ['date', 'Y-m-d']
],
],
]); ?>
```
In the above `text` corresponds to [[\yii\base\Formatter::asText()]]. The value of the column is passed as the first
argument. In the second column definition `date` corresponds to [[\yii\base\Formatter::asDate()]]. The value of the
column is, again, passed as the first argument while 'Y-m-d' is used as the second argument value.
Here's the bundled formatters list:
- [[\yii\base\Formatter::asRaw()|raw]] - the value is outputted as is.
- [[\yii\base\Formatter::asText()|text]] - the value is HTML-encoded. This format is used by default.
- [[\yii\base\Formatter::asNtext()|ntext]] - the value is formatted as an HTML-encoded plain text with newlines converted
into line breaks.
- [[\yii\base\Formatter::asParagraphs()|paragraphs]] - the value is formatted as HTML-encoded text paragraphs wrapped
into `<p>` tags.
- [[\yii\base\Formatter::asHtml()|html]] - the value is purified using [[HtmlPurifier]] to avoid XSS attacks. You can
pass additional options such as `['html', ['Attr.AllowedFrameTargets' => ['_blank']]]`.
- [[\yii\base\Formatter::asEmail()|email]] - the value is formatted as a mailto link.
- [[\yii\base\Formatter::asImage()|image]] - the value is formatted as an image tag.
- [[\yii\base\Formatter::asUrl()|url]] - the value is formatted as a hyperlink.
- [[\yii\base\Formatter::asBoolean()|boolean]] - the value is formatted as a boolean. You can set what's rendered for
true and false values by calling `Yii::$app->formatter->booleanFormat = ['No', 'Yes'];` before outputting GridView.
- [[\yii\base\Formatter::asDate()|date]] - the value is formatted as date.
- [[\yii\base\Formatter::asTime()|time]] - the value is formatted as time.
- [[\yii\base\Formatter::asDatetime()|datetime]] - the value is formatted as datetime.
- [[\yii\base\Formatter::asInteger()|integer]] - the value is formatted as an integer.
- [[\yii\base\Formatter::asDouble()|double]] - the value is formatted as a double number.
- [[\yii\base\Formatter::asNumber()|number]] - the value is formatted as a number with decimal and thousand separators.
- [[\yii\base\Formatter::asSize()|size]] - the value that is a number of bytes is formatted as a human readable size.
- [[\yii\base\Formatter::asRelativeTime()|relativeTime]] - the value is formatted as the time interval between a date
and now in human readable form.
#### Action column
......
......@@ -179,6 +179,7 @@ server {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $uri =404;
}
location ~ /\.(ht|svn|git) {
......
......@@ -214,7 +214,7 @@ For example, the following application configuration makes sure the `debug` modu
Modules can be nested in unlimited levels. That is, a module can contain another module which can contain yet
another module. We call the former *parent module* while the latter *child module*. Child modules must be declared
in the [[yii\bas\Module::modules|modules]] property of their parent modules. For example,
in the [[yii\base\Module::modules|modules]] property of their parent modules. For example,
```php
namespace app\modules\forum;
......
......@@ -7,5 +7,20 @@ Acceptance Tests
- https://github.com/yiisoft/yii2/blob/master/apps/advanced/README.md#testing
- https://github.com/yiisoft/yii2/blob/master/apps/basic/tests/README.md
How to run php-server
---------------------
How to run webserver
--------------------
In order to perform acceptance tests you need a web server. Since PHP 5.4 has built-in one, we can use it. For the basic
application template it would be:
```
cd web
php -S localhost:8080
```
In order for the tests to work correctly you need to adjust `TEST_ENTRY_URL` in `_bootstrap.php` file. It should point
to `index-test.php` of your webserver. Since we're running directly from its directory the line would be:
```php
defined('TEST_ENTRY_URL') or define('TEST_ENTRY_URL', '/index-test.php');
```
......@@ -106,9 +106,9 @@ Aliased class import:
{{ use({'alias' => '/app/widgets/MyWidget'}) }}
```
#### Referencing other views
#### Referencing other templates
There are two ways of referencing views in `include` and `extends` statements:
There are two ways of referencing templates in `include` and `extends` statements:
```
{% include "comment.twig" %}
......@@ -118,8 +118,8 @@ There are two ways of referencing views in `include` and `extends` statements:
{% extends "@app/views/layouts/2columns.twig" %}
```
In the first case the view will be searched relatively to the path current view is in. For `comment.twig` and `post.twig`
that means these will be searched in the same directory as the view that's rendered currently.
In the first case the view will be searched relatively to the current template path. For `comment.twig` and `post.twig`
that means these will be searched in the same directory as the currently rendered template.
In the second case we're using path aliases. All the Yii aliases such as `@app` are available by default.
......@@ -264,8 +264,9 @@ Then in the template you can apply filter using the following syntax:
Smarty
------
To use Smarty, you need to create templates in files that have the `.tpl` extension (or use another file extension but configure the component accordingly). Unlike standard view files, when using Smarty you must include the extension in your `$this->render()`
or `$this->renderPartial()` controller calls:
To use Smarty, you need to create templates in files that have the `.tpl` extension (or use another file extension but
configure the component accordingly). Unlike standard view files, when using Smarty you must include the extension in
your `$this->render()` or `$this->renderPartial()` controller calls:
```php
return $this->render('renderer.tpl', ['username' => 'Alex']);
......@@ -277,20 +278,173 @@ The best resource to learn Smarty template syntax is its official documentation
[www.smarty.net](http://www.smarty.net/docs/en/). Additionally there are Yii-specific syntax extensions
described below.
#### Additional functions
#### Setting object properties
There's a special function called `set` that allows you to set common properties of the view and controller. Currently
available properties are `title`, `theme` and `layout`:
```
{set title="My Page"}
{set theme="frontend"}
{set layout="main.tpl"}
```
For title there's dedicated block as well:
```
{title}My Page{/title}
```
#### Setting meta tags
Meta tags could be set like to following:
```
{meta keywords="Yii,PHP,Smarty,framework"}
```
There's also dedicated block for description:
```
{description}This is my page about Smarty extension{/description}
```
#### Calling object methods
Sometimes you need calling
#### Importing static classes, using widgets as functions and blocks
You can import additional static classes right in the template:
```
{use class="yii\helpers\Html"}
{Html::mailto('eugenia@example.com')}
```
If you want you can set custom alias:
```
{use class="yii\helpers\Html" as="Markup"}
{Markup::mailto('eugenia@example.com')}
```
Extension helps using widgets in convenient way converting their syntax to function calls or blocks. For regular widgets
function could be used like the following:
```
{use class='@yii\grid\GridView' type='function'}
{GridView dataProvider=$provider}
```
For widgets with `begin` and `end` methods such as ActiveForm it's better to use block:
```
{use class='yii\widgets\ActiveForm' type='block'}
{ActiveForm assign='form' id='login-form' action='/form-handler' options=['class' => 'form-horizontal']}
{$form->field($model, 'firstName')}
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<input type="submit" value="Login" class="btn btn-primary" />
</div>
</div>
{/ActiveForm}
```
If you're using particular widget a lot, it is a good idea to declare it in application config and remove `{use class`
call from templates:
Yii adds the following construct to the standard Smarty syntax:
```php
'components' => [
'view' => [
// ...
'renderers' => [
'tpl' => [
'class' => 'yii\smarty\ViewRenderer',
'widgets' => [
'blocks' => [
'ActiveForm' => '\yii\widgets\ActiveForm',
],
],
],
],
],
],
```
#### Referencing other templates
There are two main ways of referencing templates in `include` and `extends` statements:
```
{include 'comment.tpl'}
{extends 'post.tpl'}
{include '@app/views/snippets/avatar.tpl'}
{extends '@app/views/layouts/2columns.tpl'}
```
In the first case the view will be searched relatively to the current template path. For `comment.tpl` and `post.tpl`
that means these will be searched in the same directory as the currently rendered template.
In the second case we're using path aliases. All the Yii aliases such as `@app` are available by default.
#### CSS, JavaScript and asset bundles
In order to register JavaScript and CSS files the following syntax could be used:
```
{registerJsFile url='http://maps.google.com/maps/api/js?sensor=false' position='POS_END'}
{registerCssFile url='@assets/css/normalizer.css'}
```
If you need JavaScript and CSS directly in the template there are convenient blocks:
```
{registerJs key='show' position='POS_LOAD'}
$("span.show").replaceWith('<div class="show">');
{/registerJs}
{registerCss}
div.header {
background-color: #3366bd;
color: white;
}
{/registerCss}
```
Asset bundles could be registered the following way:
```
{use class="yii\web\JqueryAsset"}
{JqueryAsset::register($this)|void}
```
Here we're using `void` modifier because we don't need method call result.
#### URLs
There are two functions you can use for building URLs:
```php
<a href="{path route='blog/view' alias=$post.alias}">{$post.title}</a>
<a href="{url route='blog/view' alias=$post.alias}">{$post.title}</a>
```
Internally, the `path()` function calls Yii's `Url::to()` method.
`path` generates relative URL while `url` generates absolute one. Internally both are using [[\yii\helpers\Url]].
#### Additional variables
Within Smarty templates, you can also make use of these variables:
Within Smarty templates the following variables are always defined:
- `$app`, which equates to `\Yii::$app`
- `$this`, which equates to the current `View` object
#### Accessing config params
Yii parameters that are available in your application through `Yii::$app->params->something` could be used the following
way:
```
`{#something#}`
```
......@@ -140,7 +140,7 @@ class Alert extends Widget
Html::addCssClass($this->options, 'fade');
Html::addCssClass($this->options, 'in');
if ($this->closeButton !== null) {
if ($this->closeButton !== false) {
$this->closeButton = array_merge([
'data-dismiss' => 'alert',
'aria-hidden' => 'true',
......
......@@ -5,6 +5,7 @@ Yii Framework 2 composer extension Change Log
--------------------------
- Bug #3438: Fixed support for non-lowercase package names (cebe)
- Enh #4597: `yii\composer\Installer::setPermission()` supports setting permission for both directories and files now (qiangxue)
2.0.0-beta April 13, 2014
-------------------------
......
......@@ -237,24 +237,22 @@ EOF
foreach ((array) $options[self::EXTRA_WRITABLE] as $path) {
echo "Setting writable: $path ...";
if (is_dir($path)) {
chmod($path, 0777);
if (is_dir($path) || is_file($path)) {
chmod($path, is_file($path) ? 0666 : 0777);
echo "done\n";
} else {
echo "The directory was not found: " . getcwd() . DIRECTORY_SEPARATOR . $path;
echo "The directory or file was not found: " . getcwd() . DIRECTORY_SEPARATOR . $path;
return;
}
}
foreach ((array) $options[self::EXTRA_EXECUTABLE] as $path) {
echo "Setting executable: $path ...";
if (is_file($path)) {
if (is_dir($path) || is_file($path)) {
chmod($path, 0755);
echo "done\n";
} else {
echo "\n\tThe file was not found: " . getcwd() . DIRECTORY_SEPARATOR . $path . "\n";
echo "\n\tThe directory or file was not found: " . getcwd() . DIRECTORY_SEPARATOR . $path . "\n";
return;
}
}
......
......@@ -70,6 +70,7 @@ class ActiveRecord extends BaseActiveRecord
/**
* @inheritdoc
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
public static function find()
{
......
......@@ -227,7 +227,7 @@ class QueryBuilder extends \yii\base\Object
return count($parts) === 1 ? $parts[0] : ['and' => $parts];
}
private function buildNotCondition($operator, $operands, &$params)
private function buildNotCondition($operator, $operands)
{
if (count($operands) != 1) {
throw new InvalidParamException("Operator '$operator' requires exactly one operand.");
......@@ -235,7 +235,7 @@ class QueryBuilder extends \yii\base\Object
$operand = reset($operands);
if (is_array($operand)) {
$operand = $this->buildCondition($operand, $params);
$operand = $this->buildCondition($operand);
}
return [$operator => $operand];
......
......@@ -94,6 +94,7 @@ abstract class ActiveRecord extends BaseActiveRecord
/**
* @inheritdoc
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
public static function find()
{
......
......@@ -47,6 +47,7 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord
{
/**
* @inheritdoc
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
public static function find()
{
......
......@@ -51,6 +51,7 @@ class ActiveRecord extends BaseActiveRecord
/**
* @inheritdoc
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
public static function find()
{
......
......@@ -4,8 +4,26 @@ Yii Framework 2 smarty extension Change Log
2.0.0-rc under development
--------------------------
- no changes in this release.
- Enh #4619 (samdark, hwmaier)
- New functions:
- `url` generates absolute URL.
- `set` allows setting commonly used view paramters: `title`, `theme` and `layout`.
- `meta` registers meta tag.
- `registerJsFile` registers JavaScript file.
- `registerCssFile` registers CSS file.
- `use` allows importing classes to the template and optionally provides these as functions and blocks.
- New blocks:
- `title`.
- `description`.
- `registerJs`.
- `registerCss`.
- New modifier `void` that allows calling functions and ignoring result.
- Moved most of Yii custom syntax into `\yii\smarty\Extension` class that could be extended via `extensionClass` property.
- Added ability to set Smarty options via config using `options`.
- Added `imports` property that accepts an array of classes imported into template namespace.
- Added `widgets` property that can be used to import widgets as Smarty tags.
- `Yii::$app->params['paramKey']` values are now accessible as Smarty config variables `{#paramKey#}`.
- Added ability to use Yii aliases in `extends` and `require`.
2.0.0-beta April 13, 2014
-------------------------
......
This diff is collapsed.
......@@ -39,3 +39,5 @@ or add
```
to the require section of your composer.json.
Note that the smarty composer package is distributed using subversion so you may need to install subversion.
This diff is collapsed.
......@@ -138,6 +138,7 @@ abstract class ActiveRecord extends BaseActiveRecord
/**
* @inheritdoc
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
public static function find()
{
......
......@@ -22,7 +22,7 @@ abstract class Template extends \Twig_Template
// Twig uses isset() to check if attribute exists which does not work when attribute exists but is null
if ($object instanceof \yii\db\BaseActiveRecord) {
if ($type === \Twig_Template::METHOD_CALL) {
return $object->$item();
return $object->$item($arguments);
} else {
return $object->$item;
}
......
......@@ -57,6 +57,7 @@ Yii Framework 2 Change Log
- Bug #3863: Fixed incorrect js selector for `\yii\widgets\ActiveForm::errorSummaryCssClass` when it contains multiple classes (creocoder, umneeq)
- Bug #3893: Headers did not overwrite default setting by webserver (cebe)
- Bug #3909: `Html::to()` should not prefix base URL to URLs that already contain scheme (qiangxue)
- Bug #3920: Fixed issue with loading default values of PostgreSQL boolean columns (cebe)
- Bug #3934: yii.handleAction() in yii.js does not correctly detect if a hyperlink contains useful URL or not (joni-jones, qiangxue)
- Bug #3968: Messages logged in shutdown functions are not handled (qiangxue)
- Bug #3989: Fixed yii\log\FileTarget::$rotateByCopy to avoid any rename (cebe)
......@@ -69,6 +70,7 @@ Yii Framework 2 Change Log
- Bug #4241: `yii\widgets\Pjax` was incorrectly setting container id (mitalcoi)
- Bug #4276: Added check for UPLOAD_ERR_NO_FILE in `yii\web\UploadedFile` and return null if no file was uploaded (OmgDef)
- Bug #4342: mssql (dblib) driver does not support getting attributes (tof06)
- Bug #4371: Active form client validation wasn't working in case of two models having same named fields (abrahamy)
- Bug #4409: Upper case letters in subdirectory prefixes of controller IDs were not properly handled (qiangxue)
- Bug #4412: Formatter used SI Prefixes for base 1024, now uses binary prefixes (kmindi)
- Bug #4427: Formatter could do one division too much (kmindi)
......@@ -79,6 +81,7 @@ Yii Framework 2 Change Log
- Bug #4514: Fixed Request class crashing when empty CSRF token value is sent in cookie (cebe)
- Bug #4519: `yii\base\Model::isAttributeRequired()` should check if the `when` option of the validator is set (thiagotalma)
- Bug #4592: Fixed `yii help` command was listing incorrect action names for methods like `actionSayNO` (samdark)
- Bug #4654: Fixed issue with PostgreSQL and inserting boolean values with batch insert (cebe)
- Bug: Fixed inconsistent return of `\yii\console\Application::runAction()` (samdark)
- Bug: URL encoding for the route parameter added to `\yii\web\UrlManager` (klimov-paul)
- Bug: Fixed the bug that requesting protected or private action methods would cause 500 error instead of 404 (qiangxue)
......@@ -157,6 +160,7 @@ Yii Framework 2 Change Log
- Enh #4080: Added proper handling and support of the symlinked directories in `FileHelper`, added $options parameter in `FileHelper::removeDirectory()` (resurtm)
- Enh #4086: changedAttributes of afterSave Event now contain old values (dizews)
- Enh #4114: Added `Security::generateRandomBytes()`, improved tests (samdark)
- Enh #4122: `Html::error()` and `Html::errorSummary()` are now accepting `encode` option. If set to false it prevents encoding of error messages (samdark)
- Enh #4131: Security adjustments (tom--)
- Added HKDF to `yii\base\Security`.
- Reverted auto fallback to PHP PBKDF2.
......@@ -172,7 +176,13 @@ Yii Framework 2 Change Log
- Enh #4559: Added `beforeValidateAll` and `afterValidateAll` callbacks to `ActiveForm` (Alex-Code)
- Enh #4566: Added client validation support for image validator (Skysplit, qiangxue)
- Enh #4581: Added ability to disable url encoding in `UrlRule` (tadaszelvys)
- Enh #4597: `yii\composer\Installer::setPermission()` supports setting permission for both directories and files now (qiangxue)
- Enh #4602: Added $key param in ActionColumn buttons Closure call (disem)
- Enh #4607: AR model will throw an exception if it does not have a primary key to avoid updating/deleting data massively (qiangxue)
- Enh #4630: Added automatic generating of unique slug value to `yii\behaviors\Sluggable` (klimov-paul)
- Enh #4644: Added `\yii\db\Schema::createColumnSchema()` to be able to customize column schema used (mcd-php)
- Enh #4656: HtmlPurifier helper config can now be a closure to change the purifier config object after it was created (Alex-Code)
- Enh #4691: Encoding on `ActiveForm` and `ActiveField` validation errors is now configurable (Alex-Code)
- Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
- Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue)
- Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue)
......@@ -218,6 +228,7 @@ Yii Framework 2 Change Log
- Chg #4310: Removed `$data` from signature of `yii\rbac\ManagerInterface` (samdark)
- Chg #4318: `yii\helpers\Html::ul()` and `ol()` will return an empty list tag if an empty item array is given (qiangxue)
- Chg #4331: `yii\helpers\Url` now uses `UrlManager` to determine base URL when generating URLs (qiangxue)
- Chg #4586: Signed bigint and unsigned int will be converted into integers when they are loaded from DB by AR (qiangxue)
- Chg #4591: `yii\helpers\Url::to()` will no longer prefix relative URLs with the base URL (qiangxue)
- Chg #4595: `yii\widgets\LinkPager`'s `nextPageLabel`, `prevPageLabel`, `firstPageLabel`, `lastPageLabel` are now taking `false` instead of `null` for "no label" (samdark)
- Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue)
......@@ -232,6 +243,7 @@ Yii Framework 2 Change Log
- New #3911: Added `yii\behaviors\SluggableBehavior` that fills the specified model attribute with the transliterated and adjusted version to use in URLs (creocoder)
- New #4193: Added `yii\filters\Cors` CORS filter to allow Cross Origin Resource Sharing (pgaultier)
- New: Added `yii\base\InvalidValueException` (qiangxue)
- New: Added `yii\caching\ArrayCache` (cebe)
2.0.0-beta April 13, 2014
......
......@@ -23,6 +23,8 @@
};
var defaults = {
// whether to encode the error summary
encodeErrorSummary: true,
// the jQuery selector for the error summary
errorSummary: undefined,
// whether to perform validation before submitting the form.
......@@ -73,6 +75,8 @@
input: undefined,
// the jQuery selector of the error tag
error: undefined,
// whether to encode the error
encodeError: true,
// whether to perform validation when a change is detected on the input
validateOnChange: false,
// whether to perform validation when the user is typing.
......@@ -404,11 +408,15 @@
var $container = $form.find(attribute.container);
var $error = $container.find(attribute.error);
if (hasError) {
$error.text(messages[attribute.id][0]);
if (attribute.encodeError) {
$error.text(messages[attribute.id][0]);
} else {
$error.html(messages[attribute.id][0]);
}
$container.removeClass(data.settings.validatingCssClass + ' ' + data.settings.successCssClass)
.addClass(data.settings.errorCssClass);
} else {
$error.text('');
$error.empty();
$container.removeClass(data.settings.validatingCssClass + ' ' + data.settings.errorCssClass + ' ')
.addClass(data.settings.successCssClass);
}
......@@ -425,12 +433,18 @@
var updateSummary = function ($form, messages) {
var data = $form.data('yiiActiveForm'),
$summary = $form.find(data.settings.errorSummary),
$ul = $summary.find('ul').html('');
$ul = $summary.find('ul').empty();
if ($summary.length && messages) {
$.each(data.attributes, function () {
if ($.isArray(messages[this.id]) && messages[this.id].length) {
$ul.append($('<li/>').text(messages[this.id][0]));
var error = $('<li/>');
if (data.settings.encodeErrorSummary) {
error.text(messages[this.id][0]);
} else {
error.html(messages[this.id][0]);
}
$ul.append(error);
}
});
$summary.toggle($ul.find('li').length > 0);
......
......@@ -516,7 +516,7 @@ class Security extends Component
* @throws Exception on bad password parameter or cost parameter
* @throws InvalidConfigException
* @return string The password hash string. When [[passwordHashStrategy]] is set to 'crypt',
* the output is alwaus 60 ASCII characters, when set to 'password_hash' the output length
* the output is always 60 ASCII characters, when set to 'password_hash' the output length
* might increase in future versions of PHP (http://php.net/manual/en/function.password-hash.php)
* @see validatePassword()
*/
......
......@@ -10,6 +10,8 @@ namespace yii\behaviors;
use yii\base\InvalidConfigException;
use yii\db\BaseActiveRecord;
use yii\helpers\Inflector;
use yii\validators\UniqueValidator;
use Yii;
/**
* SluggableBehavior automatically fills the specified attribute with a value that can be used a slug in a URL.
......@@ -46,7 +48,9 @@ use yii\helpers\Inflector;
* ];
* }
* ```
*
* @author Alexander Kochetov <creocoder@gmail.com>
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class SluggableBehavior extends AttributeBehavior
......@@ -56,7 +60,7 @@ class SluggableBehavior extends AttributeBehavior
*/
public $slugAttribute = 'slug';
/**
* @var string the attribute whose value will be converted into a slug
* @var string|array the attribute or list of attributes whose value will be converted into a slug
*/
public $attribute;
/**
......@@ -72,6 +76,32 @@ class SluggableBehavior extends AttributeBehavior
* ```
*/
public $value;
/**
* @var boolean whether to ensure generated slug value to be unique among owner class records.
* If enabled behavior will validate slug uniqueness automatically. If validation fails it will attempt
* generating unique slug value from based one until success.
*/
public $ensureUnique = false;
/**
* @var array configuration for slug uniqueness validator. Parameter 'class' may be omitted - by default
* [[UniqueValidator]] will be used.
* @see UniqueValidator
*/
public $uniqueValidator = [];
/**
* @var callable slug unique value generator. It is used in case [[ensureUnique]] enabled and generated
* slug is not unique. This should be a PHP callable with following signature:
*
* ```php
* function ($baseSlug, $iteration, $model)
* {
* // return uniqueSlug
* }
* ```
*
* If not set unique slug will be generated adding incrementing suffix to the base slug.
*/
public $uniqueSlugGenerator;
/**
......@@ -95,10 +125,83 @@ class SluggableBehavior extends AttributeBehavior
*/
protected function getValue($event)
{
$isNewSlug = true;
if ($this->attribute !== null) {
$this->value = Inflector::slug($this->owner->{$this->attribute});
$attributes = (array)$this->attribute;
/* @var $owner BaseActiveRecord */
$owner = $this->owner;
if (!$owner->getIsNewRecord() && !empty($owner->{$this->slugAttribute})) {
$isNewSlug = false;
foreach ($attributes as $attribute) {
if ($owner->isAttributeChanged($attribute)) {
$isNewSlug = true;
break;
}
}
}
if ($isNewSlug) {
$slugParts = [];
foreach ($attributes as $attribute) {
$slugParts[] = $owner->{$attribute};
}
$slug = Inflector::slug(implode('-', $slugParts));
} else {
$slug = $owner->{$this->slugAttribute};
}
} else {
$slug = parent::getValue($event);
}
return parent::getValue($event);
if ($this->ensureUnique && $isNewSlug) {
$baseSlug = $slug;
$iteration = 0;
while (!$this->validateSlug($slug)) {
$iteration++;
$slug = $this->generateUniqueSlug($baseSlug, $iteration);
}
}
return $slug;
}
/**
* Checks if given slug value is unique.
* @param string $slug slug value
* @return boolean whether slug is unique.
*/
private function validateSlug($slug)
{
/* @var $validator UniqueValidator */
/* @var $model BaseActiveRecord */
$validator = Yii::createObject(array_merge(
[
'class' => UniqueValidator::className()
],
$this->uniqueValidator
));
$model = clone $this->owner;
$model->clearErrors();
$model->{$this->slugAttribute} = $slug;
$validator->validateAttribute($model, $this->slugAttribute);
return !$model->hasErrors();
}
/**
* Generates slug using configured callback or increment of iteration.
* @param string $baseSlug base slug value
* @param integer $iteration iteration number
* @return string new slug value
* @throws \yii\base\InvalidConfigException
*/
private function generateUniqueSlug($baseSlug, $iteration)
{
if (is_callable($this->uniqueSlugGenerator)) {
return call_user_func($this->uniqueSlugGenerator, $baseSlug, $iteration, $this->owner);
} else {
return $baseSlug . '-' . ($iteration + 1);
}
}
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* ArrayCache provides caching for the current request only by storing the values in an array.
*
* See [[Cache]] for common cache operations that ArrayCache supports.
*
* Unlike the [[Cache]], ArrayCache allows the expire parameter of [[set]], [[add]], [[mset]] and [[madd]] to
* be a floating point number, so you may specify the time in milliseconds (e.g. 0.1 will be 100 milliseconds).
*
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
*/
class ArrayCache extends Cache
{
private $_cache;
/**
* @inheritdoc
*/
public function exists($key)
{
$key = $this->buildKey($key);
return isset($this->_cache[$key]) && ($this->_cache[$key][1] === 0 || $this->_cache[$key][1] > microtime(true));
}
/**
* @inheritdoc
*/
protected function getValue($key)
{
if (isset($this->_cache[$key]) && ($this->_cache[$key][1] === 0 || $this->_cache[$key][1] > microtime(true))) {
return $this->_cache[$key][0];
} else {
return false;
}
}
/**
* @inheritdoc
*/
protected function setValue($key, $value, $duration)
{
$this->_cache[$key] = [$value, $duration === 0 ? 0 : microtime(true) + $duration];
return true;
}
/**
* @inheritdoc
*/
protected function addValue($key, $value, $duration)
{
if (isset($this->_cache[$key]) && ($this->_cache[$key][1] === 0 || $this->_cache[$key][1] > microtime(true))) {
return false;
} else {
$this->_cache[$key] = [$value, $duration === 0 ? 0 : microtime(true) + $duration];
return true;
}
}
/**
* @inheritdoc
*/
protected function deleteValue($key)
{
unset($this->_cache[$key]);
return true;
}
/**
* @inheritdoc
*/
protected function flushValues()
{
$this->_cache = [];
return true;
}
}
......@@ -256,6 +256,7 @@ class ActiveRecord extends BaseActiveRecord
/**
* @inheritdoc
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
public static function find()
{
......@@ -282,7 +283,7 @@ class ActiveRecord extends BaseActiveRecord
*/
public static function getTableSchema()
{
$schema = static::getDb()->getTableSchema(static::tableName());
$schema = static::getDb()->getSchema()->getTableSchema(static::tableName());
if ($schema !== null) {
return $schema;
} else {
......
......@@ -1008,10 +1008,14 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
* @return mixed the old primary key value. An array (column name => column value) is returned if the primary key
* is composite or `$asArray` is true. A string is returned otherwise (null will be returned if
* the key value is null).
* @throws Exception if the AR model does not have a primary key
*/
public function getOldPrimaryKey($asArray = false)
{
$keys = $this->primaryKey();
if (empty($keys)) {
throw new Exception(get_class($this) . ' does not have a primary key. You should either define a primary key for the corresponding table or override the primaryKey() method.');
}
if (count($keys) === 1 && !$asArray) {
return isset($this->_oldAttributes[$keys[0]]) ? $this->_oldAttributes[$keys[0]] : null;
} else {
......
......@@ -131,7 +131,8 @@ class QueryBuilder extends \yii\base\Object
*/
public function insert($table, $columns, &$params)
{
if (($tableSchema = $this->db->getTableSchema($table)) !== null) {
$schema = $this->db->getSchema();
if (($tableSchema = $schema->getTableSchema($table)) !== null) {
$columnSchemas = $tableSchema->columns;
} else {
$columnSchemas = [];
......@@ -139,7 +140,7 @@ class QueryBuilder extends \yii\base\Object
$names = [];
$placeholders = [];
foreach ($columns as $name => $value) {
$names[] = $this->db->quoteColumnName($name);
$names[] = $schema->quoteColumnName($name);
if ($value instanceof Expression) {
$placeholders[] = $value->expression;
foreach ($value->params as $n => $v) {
......@@ -152,7 +153,7 @@ class QueryBuilder extends \yii\base\Object
}
}
return 'INSERT INTO ' . $this->db->quoteTableName($table)
return 'INSERT INTO ' . $schema->quoteTableName($table)
. ' (' . implode(', ', $names) . ') VALUES ('
. implode(', ', $placeholders) . ')';
}
......@@ -178,7 +179,8 @@ class QueryBuilder extends \yii\base\Object
*/
public function batchInsert($table, $columns, $rows)
{
if (($tableSchema = $this->db->getTableSchema($table)) !== null) {
$schema = $this->db->getSchema();
if (($tableSchema = $schema->getTableSchema($table)) !== null) {
$columnSchemas = $tableSchema->columns;
} else {
$columnSchemas = [];
......@@ -192,7 +194,7 @@ class QueryBuilder extends \yii\base\Object
$value = $columnSchemas[$columns[$i]]->dbTypecast($value);
}
if (is_string($value)) {
$value = $this->db->quoteValue($value);
$value = $schema->quoteValue($value);
} elseif ($value === false) {
$value = 0;
} elseif ($value === null) {
......@@ -204,10 +206,10 @@ class QueryBuilder extends \yii\base\Object
}
foreach ($columns as $i => $name) {
$columns[$i] = $this->db->quoteColumnName($name);
$columns[$i] = $schema->quoteColumnName($name);
}
return 'INSERT INTO ' . $this->db->quoteTableName($table)
return 'INSERT INTO ' . $schema->quoteTableName($table)
. ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values);
}
......
......@@ -85,6 +85,15 @@ abstract class Schema extends Object
private $_builder;
/**
* @return \yii\db\ColumnSchema
* @throws \yii\base\InvalidConfigException
*/
protected function createColumnSchema()
{
return Yii::createObject('yii\db\ColumnSchema');
}
/**
* Loads the metadata for the specified table.
* @param string $name table name
......@@ -481,13 +490,16 @@ abstract class Schema extends Object
// abstract type => php type
'smallint' => 'integer',
'integer' => 'integer',
'bigint' => 'integer',
'boolean' => 'boolean',
'float' => 'double',
'binary' => 'resource',
];
if (isset($typeMap[$column->type])) {
if ($column->type === 'integer') {
return $column->unsigned ? 'string' : 'integer';
if ($column->type === 'bigint') {
return PHP_INT_SIZE == 8 && !$column->unsigned ? 'integer' : 'string';
} elseif ($column->type === 'integer') {
return PHP_INT_SIZE == 4 && $column->unsigned ? 'string' : 'integer';
} else {
return $typeMap[$column->type];
}
......
......@@ -195,7 +195,7 @@ class Schema extends \yii\db\Schema
*/
protected function loadColumnSchema($info)
{
$column = new ColumnSchema();
$column = $this->createColumnSchema();
$column->name = $info['Field'];
$column->allowNull = $info['Null'] === 'YES';
......
......@@ -176,7 +176,7 @@ class Schema extends \yii\db\Schema
*/
protected function loadColumnSchema($info)
{
$column = new ColumnSchema();
$column = $this->createColumnSchema();
$column->name = $info['column_name'];
$column->allowNull = $info['is_nullable'] == 'YES';
......
......@@ -127,7 +127,7 @@ class Schema extends \yii\db\Schema
*/
protected function loadColumnSchema($info)
{
$column = new ColumnSchema;
$column = $this->createColumnSchema();
$column->name = $info['Field'];
$column->allowNull = $info['Null'] === 'YES';
......
......@@ -200,7 +200,7 @@ EOD;
*/
protected function createColumn($column)
{
$c = new ColumnSchema();
$c = $this->createColumnSchema();
$c->name = $column['COLUMN_NAME'];
$c->allowNull = $column['NULLABLE'] === 'Y';
$c->isPrimaryKey = strpos($column['KEY'], 'P') !== false;
......
......@@ -128,8 +128,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
public function checkIntegrity($check = true, $schema = '', $table = '')
{
$enable = $check ? 'ENABLE' : 'DISABLE';
$schema = $schema ? $schema : $this->db->schema->defaultSchema;
$tableNames = $table ? [$table] : $this->db->schema->getTableNames($schema);
$schema = $schema ? $schema : $this->db->getSchema()->defaultSchema;
$tableNames = $table ? [$table] : $this->db->getSchema()->getTableNames($schema);
$command = '';
foreach ($tableNames as $tableName) {
......@@ -159,4 +159,45 @@ class QueryBuilder extends \yii\db\QueryBuilder
. $this->db->quoteColumnName($column) . ' TYPE '
. $this->getColumnType($type);
}
/**
* @inheritdoc
*/
public function batchInsert($table, $columns, $rows)
{
$schema = $this->db->getSchema();
if (($tableSchema = $schema->getTableSchema($table)) !== null) {
$columnSchemas = $tableSchema->columns;
} else {
$columnSchemas = [];
}
$values = [];
foreach ($rows as $row) {
$vs = [];
foreach ($row as $i => $value) {
if (!is_array($value) && isset($columnSchemas[$columns[$i]])) {
$value = $columnSchemas[$columns[$i]]->dbTypecast($value);
}
if (is_string($value)) {
$value = $schema->quoteValue($value);
} elseif ($value === true) {
$value = 'TRUE';
} elseif ($value === false) {
$value = 'FALSE';
} elseif ($value === null) {
$value = 'NULL';
}
$vs[] = $value;
}
$values[] = '(' . implode(', ', $vs) . ')';
}
foreach ($columns as $i => $name) {
$columns[$i] = $schema->quoteColumnName($name);
}
return 'INSERT INTO ' . $schema->quoteTableName($table)
. ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values);
}
}
......@@ -412,6 +412,8 @@ SQL;
} elseif ($column->defaultValue) {
if ($column->type === 'timestamp' && $column->defaultValue === 'now()') {
$column->defaultValue = new Expression($column->defaultValue);
} elseif ($column->type === 'boolean') {
$column->defaultValue = ($column->defaultValue === 'true');
} elseif (stripos($column->dbType, 'bit') === 0 || stripos($column->dbType, 'varbit') === 0) {
$column->defaultValue = bindec(trim($column->defaultValue, 'B\''));
} elseif (preg_match("/^'(.*?)'::/", $column->defaultValue, $matches)) {
......@@ -434,7 +436,7 @@ SQL;
*/
protected function loadColumnSchema($info)
{
$column = new ColumnSchema();
$column = $this->createColumnSchema();
$column->allowNull = $info['is_nullable'];
$column->autoIncrement = $info['is_autoinc'];
$column->comment = $info['column_comment'];
......
......@@ -70,7 +70,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
return parent::batchInsert($table, $columns, $rows);
}
if (($tableSchema = $this->db->getTableSchema($table)) !== null) {
$schema = $this->db->getSchema();
if (($tableSchema = $schema->getTableSchema($table)) !== null) {
$columnSchemas = $tableSchema->columns;
} else {
$columnSchemas = [];
......@@ -84,7 +85,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
$value = $columnSchemas[$columns[$i]]->dbTypecast($value);
}
if (is_string($value)) {
$value = $this->db->quoteValue($value);
$value = $schema->quoteValue($value);
} elseif ($value === false) {
$value = 0;
} elseif ($value === null) {
......@@ -96,10 +97,10 @@ class QueryBuilder extends \yii\db\QueryBuilder
}
foreach ($columns as $i => $name) {
$columns[$i] = $this->db->quoteColumnName($name);
$columns[$i] = $schema->quoteColumnName($name);
}
return 'INSERT INTO ' . $this->db->quoteTableName($table)
return 'INSERT INTO ' . $schema->quoteTableName($table)
. ' (' . implode(', ', $columns) . ') SELECT ' . implode(' UNION SELECT ', $values);
}
......
......@@ -212,7 +212,7 @@ class Schema extends \yii\db\Schema
*/
protected function loadColumnSchema($info)
{
$column = new ColumnSchema;
$column = $this->createColumnSchema();
$column->name = $info['name'];
$column->allowNull = !$info['notnull'];
$column->isPrimaryKey = $info['pk'] != 0;
......
......@@ -1067,6 +1067,7 @@ class BaseHtml
*
* - header: string, the header HTML for the error summary. If not set, a default prompt string will be used.
* - footer: string, the footer HTML for the error summary.
* - encode: boolean, if set to false then value won't be encoded.
*
* The rest of the options will be rendered as the attributes of the container tag. The values will
* be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
......@@ -1074,6 +1075,11 @@ class BaseHtml
*/
public static function errorSummary($models, $options = [])
{
$header = isset($options['header']) ? $options['header'] : '<p>' . Yii::t('yii', 'Please fix the following errors:') . '</p>';
$footer = isset($options['footer']) ? $options['footer'] : '';
$encode = !isset($options['encode']) || $options['encode'] !== false;
unset($options['header'], $options['footer'], $options['encode']);
$lines = [];
if (!is_array($models)) {
$models = [$models];
......@@ -1081,14 +1087,10 @@ class BaseHtml
foreach ($models as $model) {
/* @var $model Model */
foreach ($model->getFirstErrors() as $error) {
$lines[] = Html::encode($error);
$lines[] = $encode ? Html::encode($error) : $error;
}
}
$header = isset($options['header']) ? $options['header'] : '<p>' . Yii::t('yii', 'Please fix the following errors:') . '</p>';
$footer = isset($options['footer']) ? $options['footer'] : '';
unset($options['header'], $options['footer']);
if (empty($lines)) {
// still render the placeholder for client-side validation use
$content = "<ul></ul>";
......@@ -1111,6 +1113,7 @@ class BaseHtml
* The following options are specially handled:
*
* - tag: this specifies the tag name. If not set, "div" will be used.
* - encode: boolean, if set to false then value won't be encoded.
*
* See [[renderTagAttributes()]] for details on how attributes are being rendered.
*
......@@ -1121,8 +1124,9 @@ class BaseHtml
$attribute = static::getAttributeName($attribute);
$error = $model->getFirstError($attribute);
$tag = isset($options['tag']) ? $options['tag'] : 'div';
unset($options['tag']);
return Html::tag($tag, Html::encode($error), $options);
$encode = !isset($options['encode']) || $options['encode'] !== false;
unset($options['tag'], $options['encode']);
return Html::tag($tag, $encode ? Html::encode($error) : $error, $options);
}
/**
......
......@@ -19,17 +19,39 @@ class BaseHtmlPurifier
{
/**
* Passes markup through HTMLPurifier making it safe to output to end user
*
* @param string $content The HTML content to purify
* @param array|\Closure|null $config The config to use for HtmlPurifier.
* If not specified or `null` the default config will be used.
* You can use an array or an anonymous function to provide configuration options:
*
* @param string $content
* @param array|null $config
* @return string
* - An array will be passed to the `HTMLPurifier_Config::create()` method.
* - An anonymous function will be called after the config was created.
* The signature should be: `function($config)` where `$config` will be an
* instance of `HTMLPurifier_Config`.
*
* Here is a usage example of such a function:
*
* ~~~
* // Allow the HTML5 data attribute `data-type` on `img` elements.
* $content = HtmlPurifier::process($content, function($config) {
* $config->getHTMLDefinition(true)
* ->addAttribute('img', 'data-type', 'Text');
* });
* ~~~
*
* @return string the purified HTML content.
*/
public static function process($content, $config = null)
{
$configInstance = \HTMLPurifier_Config::create($config);
$configInstance = \HTMLPurifier_Config::create($config instanceof \Closure ? null : $config);
$configInstance->autoFinalize = false;
$purifier=\HTMLPurifier::instance($configInstance);
$purifier->config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
if ($config instanceof \Closure) {
call_user_func($config, $configInstance);
}
return $purifier->purify($content);
}
......
......@@ -27,9 +27,9 @@ class AssetConverter extends Component implements AssetConverterInterface
* target script types (either "css" or "js") and the commands used for the conversion.
*/
public $commands = [
'less' => ['css', 'lessc {from} {to} --no-color'],
'scss' => ['css', 'sass {from} {to}'],
'sass' => ['css', 'sass {from} {to}'],
'less' => ['css', 'lessc {from} {to} --no-color --source-map'],
'scss' => ['css', 'sass {from} {to} --sourcemap'],
'sass' => ['css', 'sass {from} {to} --sourcemap'],
'styl' => ['js', 'stylus < {from} > {to}'],
'coffee' => ['js', 'coffee -p {from} > {to}'],
'ts' => ['js', 'tsc --out {to} {from}'],
......
......@@ -63,6 +63,7 @@ class ActiveField extends Component
* The following special options are recognized:
*
* - tag: the tag name of the container element. Defaults to "div".
* - encode: whether to encode the error output. Defaults to true.
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
......@@ -195,7 +196,7 @@ class ActiveField extends Component
{
$clientOptions = $this->getClientOptions();
if (!empty($clientOptions)) {
$this->form->attributes[$this->attribute] = $clientOptions;
$this->form->attributes[] = $clientOptions;
}
$inputID = Html::getInputId($this->model, $this->attribute);
......@@ -726,6 +727,7 @@ class ActiveField extends Component
} else {
$options['error'] = isset($this->errorOptions['tag']) ? $this->errorOptions['tag'] : 'span';
}
$options['encodeError'] = !isset($this->errorOptions['encode']) || $this->errorOptions['encode'] !== false;
return $options;
} else {
......
......@@ -53,6 +53,10 @@ class ActiveForm extends Widget
* @var array the default configuration used by [[field()]] when creating a new field object.
*/
public $fieldConfig;
/**
* @var boolean whether to perform encoding on the error summary.
*/
public $encodeErrorSummary = true;
/**
* @var string the default CSS class for the error summary container.
* @see errorSummary()
......@@ -239,6 +243,7 @@ class ActiveForm extends Widget
protected function getClientOptions()
{
$options = [
'encodeErrorSummary' => $this->encodeErrorSummary,
'errorSummary' => '.' . implode('.', preg_split('/\s+/', $this->errorSummaryCssClass, -1, PREG_SPLIT_NO_EMPTY)),
'validateOnSubmit' => $this->validateOnSubmit,
'errorCssClass' => $this->errorCssClass,
......@@ -276,6 +281,7 @@ class ActiveForm extends Widget
public function errorSummary($models, $options = [])
{
Html::addCssClass($options, $this->errorSummaryCssClass);
$options['encode'] = $this->encodeErrorSummary;
return Html::errorSummary($models, $options);
}
......
......@@ -15,7 +15,7 @@ DIRECTORY STRUCTURE
HOW TO RUN THE TESTS
--------------------
Make sure you have PHPUnit installed.
Make sure you have PHPUnit installed and that you installed all composer dependencies (run `composer update` in the repo base directory).
Run PHPUnit in the yii repo base directory.
......@@ -38,4 +38,14 @@ PHPUnit configuration is in `phpunit.xml.dist` in repository root folder.
You can create your own phpunit.xml to override dist config.
Database and other backend system configuration can be found in `unit/data/config.php`
adjust them to your needs to allow testing databases and caching in your environment.
\ No newline at end of file
adjust them to your needs to allow testing databases and caching in your environment.
You can override configuration values by creating a `config.local.php` file
and manipulate the `$config` variable.
For example to change MySQL username and password your `config.local.php` should
contain the following:
```php
<?php
$config['databases']['mysql']['username'] = 'yiitest';
$config['databases']['mysql']['password'] = 'changeme';
```
runtime/cache/*
\ No newline at end of file
/runtime/cache/*
/data/config.local.php
\ No newline at end of file
......@@ -7,6 +7,7 @@ namespace yiiunit\data\ar;
*
* @property int $int_col
* @property int $int_col2 DEFAULT 1
* @property int $smallint_col DEFAULT 1
* @property string $char_col
* @property string $char_col2 DEFAULT 'something'
* @property string $char_col3
......
<?php
return [
/**
* This is the configuration file for the Yii2 unit tests.
* You can override configuration values by creating a `config.local.php` file
* and manipulate the `$config` variable.
* For example to change MySQL username and password your `config.local.php` should
* contain the following:
*
<?php
$config['databases']['mysql']['username'] = 'yiitest';
$config['databases']['mysql']['password'] = 'changeme';
*/
$config = [
'databases' => [
'cubrid' => [
'dsn' => 'cubrid:dbname=demodb;host=localhost;port=33000',
......@@ -58,3 +72,9 @@ return [
'options' => [],
]
];
if (is_file(__DIR__ . '/config.local.php')) {
include(__DIR__ . '/config.local.php');
}
return $config;
\ No newline at end of file
......@@ -100,6 +100,7 @@ CREATE TABLE null_values (
CREATE TABLE "type" (
"int_col" int(11) NOT NULL,
"int_col2" int(11) DEFAULT '1',
"smallint_col" smallint DEFAULT '1',
"char_col" char(100) NOT NULL,
"char_col2" varchar(100) DEFAULT 'something',
"char_col3" string,
......
......@@ -92,6 +92,7 @@ CREATE TABLE [dbo].[null_values] (
CREATE TABLE [dbo].[type] (
[int_col] [int] NOT NULL,
[int_col2] [int] DEFAULT '1',
[smallint_col] [smallint] DEFAULT '1',
[char_col] [char](100) NOT NULL,
[char_col2] [varchar](100) DEFAULT 'something',
[char_col3] [text],
......
......@@ -110,6 +110,7 @@ CREATE TABLE null_values (
CREATE TABLE `type` (
`int_col` integer NOT NULL,
`int_col2` integer DEFAULT '1',
`smallint_col` smallint(1) DEFAULT '1',
`char_col` char(100) NOT NULL,
`char_col2` varchar(100) DEFAULT 'something',
`char_col3` text,
......
......@@ -16,6 +16,7 @@ DROP TABLE IF EXISTS "profile" CASCADE;
DROP TABLE IF EXISTS "type" CASCADE;
DROP TABLE IF EXISTS "null_values" CASCADE;
DROP TABLE IF EXISTS "constraints" CASCADE;
DROP TABLE IF EXISTS "bool_values" CASCADE;
CREATE TABLE "constraints"
(
......@@ -99,6 +100,7 @@ CREATE TABLE "null_values" (
CREATE TABLE "type" (
int_col integer NOT NULL,
int_col2 integer DEFAULT '1',
smallint_col smallint DEFAULT '1',
char_col char(100) NOT NULL,
char_col2 varchar(100) DEFAULT 'something',
char_col3 text,
......@@ -113,6 +115,13 @@ CREATE TABLE "type" (
bit_col BIT(8) NOT NULL DEFAULT B'10000010'
);
CREATE TABLE "bool_values" (
id serial not null primary key,
bool_col bool,
default_true bool not null default true,
default_false boolean not null default false
);
INSERT INTO "profile" (description) VALUES ('profile customer 1');
INSERT INTO "profile" (description) VALUES ('profile customer 3');
......
......@@ -94,6 +94,7 @@ CREATE TABLE "null_values" (
CREATE TABLE "type" (
int_col INTEGER NOT NULL,
int_col2 INTEGER DEFAULT '1',
smallint_col SMALLINT(1) DEFAULT '1',
char_col char(100) NOT NULL,
char_col2 varchar(100) DEFAULT 'something',
char_col3 text,
......
......@@ -7,9 +7,11 @@
namespace yiiunit\extensions\smarty;
use yii\helpers\FileHelper;
use yii\web\AssetManager;
use yii\web\View;
use Yii;
use yiiunit\data\base\Singer;
use yiiunit\TestCase;
/**
......@@ -19,9 +21,17 @@ class ViewRendererTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->mockApplication();
}
protected function tearDown()
{
parent::tearDown();
FileHelper::removeDirectory(Yii::getAlias('@runtime/assets'));
FileHelper::removeDirectory(Yii::getAlias('@runtime/Smarty'));
}
/**
* https://github.com/yiisoft/yii2/issues/2265
*/
......@@ -41,6 +51,47 @@ class ViewRendererTest extends TestCase
$this->assertEquals('test view Hello World!.', $content);
}
public function testLayoutAssets()
{
$view = $this->mockView();
$content = $view->renderFile('@yiiunit/extensions/smarty/views/layout.tpl');
$this->assertEquals(1, preg_match('#<script src="/assets/[0-9a-z]+/jquery\\.js"></script>\s*</body>#', $content), 'Content does not contain the jquery js:' . $content);
}
public function testChangeTitle()
{
$view = $this->mockView();
$view->title = 'Original title';
$content = $view->renderFile('@yiiunit/extensions/smarty/views/changeTitle.tpl');
$this->assertTrue(strpos($content, 'New title') !== false, 'New title should be there:' . $content);
$this->assertFalse(strpos($content, 'Original title') !== false, 'Original title should not be there:' . $content);
}
public function testForm()
{
$view = $this->mockView();
$model = new Singer();
$content = $view->renderFile('@yiiunit/extensions/smarty/views/form.tpl', ['model' => $model]);
$this->assertEquals(1, preg_match('#<form id="login-form" class="form-horizontal" action="/form-handler" method="post">.*?</form>#s', $content), 'Content does not contain form:' . $content);
}
public function testInheritance()
{
$view = $this->mockView();
$content = $view->renderFile('@yiiunit/extensions/smarty/views/extends2.tpl');
$this->assertTrue(strpos($content, 'Hello, I\'m inheritance test!') !== false, 'Hello, I\'m inheritance test! should be there:' . $content);
$this->assertTrue(strpos($content, 'extends2 block') !== false, 'extends2 block should be there:' . $content);
$this->assertFalse(strpos($content, 'extends1 block') !== false, 'extends1 block should not be there:' . $content);
$content = $view->renderFile('@yiiunit/extensions/smarty/views/extends3.tpl');
$this->assertTrue(strpos($content, 'Hello, I\'m inheritance test!') !== false, 'Hello, I\'m inheritance test! should be there:' . $content);
$this->assertTrue(strpos($content, 'extends3 block') !== false, 'extends3 block should be there:' . $content);
$this->assertFalse(strpos($content, 'extends1 block') !== false, 'extends1 block should not be there:' . $content);
}
/**
* @return View
*/
......@@ -50,6 +101,9 @@ class ViewRendererTest extends TestCase
'renderers' => [
'tpl' => [
'class' => 'yii\smarty\ViewRenderer',
'options' => [
'force_compile' => true, // always recompile templates, don't do it in production
],
],
],
'assetManager' => $this->mockAssetManager(),
......
{set title='New title'}
<title>{$this->title}</title>
\ No newline at end of file
Hello, I'm inheritance test!
{block name=test}
extends1 block
{/block}
\ No newline at end of file
{extends file="@yiiunit/extensions/smarty/views/extends1.tpl"}
{block name=test}
extends2 block
{/block}
\ No newline at end of file
{extends file="@yiiunit/extensions/smarty/views/extends1.tpl"}
{block name=test}
extends3 block
{/block}
\ No newline at end of file
{use class='yii\widgets\ActiveForm' type='block'}
{ActiveForm assign='form' id='login-form' action='/form-handler' options=['class' => 'form-horizontal']}
{$form->field($model, 'firstName')}
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<input type="submit" value="Login" class="btn btn-primary" />
</div>
</div>
{/ActiveForm}
\ No newline at end of file
{use class="yii\web\JqueryAsset"}
{JqueryAsset::register($this)|void}
{$this->beginPage()}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="{$app->charset}"/>
<title>{$this->title|escape}</title>
{$this->head()}
</head>
<body>
{$this->beginBody()}
body
{$this->endBody()}
</body>
{$this->endPage()}
\ No newline at end of file
......@@ -48,6 +48,12 @@ class SphinxTestCase extends TestCase
if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) {
$this->markTestSkipped('pdo and pdo_mysql extension are required.');
}
// check whether sphinx is running and skip tests if not.
if (preg_match('/host=([\w\d.]+)/i', $this->sphinxConfig['dsn'], $hm) && preg_match('/port=(\d+)/i', $this->sphinxConfig['dsn'], $pm)) {
if (!@stream_socket_client($hm[1] . ':' . $pm[1], $errorNumber, $errorDescription, 0.5)) {
$this->markTestSkipped('No redis server running at ' . $hm[1] . ':' . $pm[1] . ' : ' . $errorNumber . ' - ' . $errorDescription);
}
}
$config = self::getParam('sphinx');
if (!empty($config)) {
$this->sphinxConfig = $config['sphinx'];
......
<?php
namespace yiiunit\extensions\twig;
use yii\helpers\FileHelper;
use yii\web\AssetManager;
use yii\web\View;
use Yii;
......@@ -24,6 +25,12 @@ class ViewRendererTest extends DatabaseTestCase
$this->mockApplication();
}
protected function tearDown()
{
parent::tearDown();
FileHelper::removeDirectory(Yii::getAlias('@runtime/assets'));
}
/**
* https://github.com/yiisoft/yii2/issues/1755
*/
......
<?php
namespace yiiunit\framework\behaviors;
use Yii;
use yiiunit\TestCase;
use yii\db\Connection;
use yii\db\ActiveRecord;
use yii\behaviors\SluggableBehavior;
/**
* Unit test for [[\yii\behaviors\SluggableBehavior]].
* @see SluggableBehavior
*
* @group behaviors
*/
class SluggableBehaviorTest extends TestCase
{
/**
* @var Connection test db connection
*/
protected $dbConnection;
public static function setUpBeforeClass()
{
if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) {
static::markTestSkipped('PDO and SQLite extensions are required.');
}
}
public function setUp()
{
$this->mockApplication([
'components' => [
'db' => [
'class' => '\yii\db\Connection',
'dsn' => 'sqlite::memory:',
]
]
]);
$columns = [
'id' => 'pk',
'name' => 'string',
'slug' => 'string',
'category_id' => 'integer',
];
Yii::$app->getDb()->createCommand()->createTable('test_slug', $columns)->execute();
}
public function tearDown()
{
Yii::$app->getDb()->close();
parent::tearDown();
}
// Tests :
public function testSlug()
{
$model = new ActiveRecordSluggable();
$model->name = 'test name';
$model->validate();
$this->assertEquals('test-name', $model->slug);
}
/**
* @depends testSlug
*/
public function testSlugSeveralAttributes()
{
$model = new ActiveRecordSluggable();
$model->getBehavior('sluggable')->attribute = array('name', 'category_id');
$model->name = 'test';
$model->category_id = 10;
$model->validate();
$this->assertEquals('test-10', $model->slug);
}
/**
* @depends testSlug
*/
public function testUniqueByIncrement()
{
$name = 'test name';
$model = new ActiveRecordSluggableUnique();
$model->name = $name;
$model->save();
$model = new ActiveRecordSluggableUnique();
$model->sluggable->uniqueSlugGenerator = 'increment';
$model->name = $name;
$model->save();
$this->assertEquals('test-name-2', $model->slug);
}
/**
* @depends testUniqueByIncrement
*/
public function testUniqueByCallback()
{
$name = 'test name';
$model = new ActiveRecordSluggableUnique();
$model->name = $name;
$model->save();
$model = new ActiveRecordSluggableUnique();
$model->sluggable->uniqueSlugGenerator = function($baseSlug, $iteration) {return $baseSlug . '-callback';};
$model->name = $name;
$model->save();
$this->assertEquals('test-name-callback', $model->slug);
}
/**
* @depends testSlug
*/
public function testUpdateUnique()
{
$name = 'test name';
$model = new ActiveRecordSluggableUnique();
$model->name = $name;
$model->save();
$model->save();
$this->assertEquals('test-name', $model->slug);
$model = ActiveRecordSluggableUnique::find()->one();
$model->save();
$this->assertEquals('test-name', $model->slug);
$model->name = 'test-name';
$model->save();
$this->assertEquals('test-name', $model->slug);
}
}
/**
* Test Active Record class with [[SluggableBehavior]] behavior attached.
*
* @property integer $id
* @property string $name
* @property string $slug
* @property integer $category_id
*
* @property SluggableBehavior $sluggable
*/
class ActiveRecordSluggable extends ActiveRecord
{
public function behaviors()
{
return [
'sluggable' => [
'class' => SluggableBehavior::className(),
'attribute' => 'name',
],
];
}
public static function tableName()
{
return 'test_slug';
}
/**
* @return SluggableBehavior
*/
public function getSluggable()
{
return $this->getBehavior('sluggable');
}
}
class ActiveRecordSluggableUnique extends ActiveRecordSluggable
{
public function behaviors()
{
return [
'sluggable' => [
'class' => SluggableBehavior::className(),
'attribute' => 'name',
'ensureUnique' => true,
],
];
}
}
\ No newline at end of file
<?php
namespace yiiunit\framework\caching;
use yii\caching\ArrayCache;
/**
* Class for testing file cache backend
* @group caching
*/
class ArrayCacheTest extends CacheTestCase
{
private $_cacheInstance = null;
/**
* @return ArrayCache
*/
protected function getCacheInstance()
{
if ($this->_cacheInstance === null) {
$this->_cacheInstance = new ArrayCache();
}
return $this->_cacheInstance;
}
public function testExpire()
{
$cache = $this->getCacheInstance();
static::$microtime = \microtime(true);
$this->assertTrue($cache->set('expire_test', 'expire_test', 2));
static::$microtime++;
$this->assertEquals('expire_test', $cache->get('expire_test'));
static::$microtime++;
$this->assertFalse($cache->get('expire_test'));
}
public function testExpireAdd()
{
$cache = $this->getCacheInstance();
static::$microtime = \microtime(true);
$this->assertTrue($cache->add('expire_testa', 'expire_testa', 2));
static::$microtime++;
$this->assertEquals('expire_testa', $cache->get('expire_testa'));
static::$microtime++;
$this->assertFalse($cache->get('expire_testa'));
}
}
......@@ -11,8 +11,19 @@ function time()
return \yiiunit\framework\caching\CacheTestCase::$time ?: \time();
}
/**
* Mock for the microtime() function for caching classes
* @param bool $float
* @return float
*/
function microtime($float = false)
{
return \yiiunit\framework\caching\CacheTestCase::$microtime ?: \microtime($float);
}
namespace yiiunit\framework\caching;
use yii\caching\Cache;
use yiiunit\TestCase;
/**
......@@ -25,6 +36,12 @@ abstract class CacheTestCase extends TestCase
* Null means normal time() behavior.
*/
public static $time;
/**
* @var float virtual time to be returned by mocked microtime() function.
* Null means normal microtime() behavior.
*/
public static $microtime;
/**
* @return Cache
......@@ -40,6 +57,7 @@ abstract class CacheTestCase extends TestCase
protected function tearDown()
{
static::$time = null;
static::$microtime = null;
}
/**
......
......@@ -21,6 +21,11 @@ class MemCacheTest extends CacheTestCase
$this->markTestSkipped("memcache not installed. Skipping.");
}
// check whether memcached is running and skip tests if not.
if (!@stream_socket_client('127.0.0.1:11211', $errorNumber, $errorDescription, 0.5)) {
$this->markTestSkipped('No redis server running at ' . '127.0.0.1:11211' . ' : ' . $errorNumber . ' - ' . $errorDescription);
}
if ($this->_cacheInstance === null) {
$this->_cacheInstance = new MemCache();
}
......
......@@ -21,6 +21,11 @@ class MemCachedTest extends CacheTestCase
$this->markTestSkipped("memcached not installed. Skipping.");
}
// check whether memcached is running and skip tests if not.
if (!@stream_socket_client('127.0.0.1:11211', $errorNumber, $errorDescription, 0.5)) {
$this->markTestSkipped('No redis server running at ' . '127.0.0.1:11211' . ' : ' . $errorNumber . ' - ' . $errorDescription);
}
if ($this->_cacheInstance === null) {
$this->_cacheInstance = new MemCache(['useMemcached' => true]);
}
......
......@@ -573,4 +573,33 @@ class ActiveRecordTest extends DatabaseTestCase
$this->assertEquals($orderItemCount, $orderItemsWithNullFKClass::find()->count());
$this->assertEquals(5, $itemClass::find()->count());
}
public function testCastValues()
{
$model = new Type();
$model->int_col = 123;
$model->int_col2 = 456;
$model->smallint_col = 42;
$model->char_col = '1337';
$model->char_col2 = 'test';
$model->char_col3 = 'test123';
$model->float_col = 1337.42;
$model->float_col2 = 42.1337;
$model->bool_col = true;
$model->bool_col2 = false;
$model->save(false);
/* @var $model Type */
$model = Type::find()->one();
$this->assertSame(123, $model->int_col);
$this->assertSame(456, $model->int_col2);
$this->assertSame(42, $model->smallint_col);
$this->assertSame('1337', trim($model->char_col));
$this->assertSame('test', $model->char_col2);
$this->assertSame('test123', $model->char_col3);
// $this->assertSame(1337.42, $model->float_col);
// $this->assertSame(42.1337, $model->float_col2);
// $this->assertSame(true, $model->bool_col);
// $this->assertSame(false, $model->bool_col2);
}
}
......@@ -119,6 +119,18 @@ class SchemaTest extends DatabaseTestCase
'scale' => null,
'defaultValue' => 1,
],
'smallint_col' => [
'type' => 'smallint',
'dbType' => 'smallint(1)',
'phpType' => 'integer',
'allowNull' => true,
'autoIncrement' => false,
'enumValues' => null,
'size' => 1,
'precision' => 1,
'scale' => null,
'defaultValue' => 1,
],
'char_col' => [
'type' => 'string',
'dbType' => 'char(100)',
......
......@@ -45,6 +45,9 @@ class CubridSchemaTest extends SchemaTest
$columns['int_col2']['dbType'] = 'integer';
$columns['int_col2']['size'] = null;
$columns['int_col2']['precision'] = null;
$columns['smallint_col']['dbType'] = 'short';
$columns['smallint_col']['size'] = null;
$columns['smallint_col']['precision'] = null;
$columns['char_col3']['type'] = 'string';
$columns['char_col3']['dbType'] = 'varchar(1073741823)';
$columns['char_col3']['size'] = 1073741823;
......
......@@ -2,6 +2,7 @@
namespace yiiunit\framework\db\pgsql;
use yiiunit\data\ar\ActiveRecord;
use yiiunit\framework\db\ActiveRecordTest;
/**
......@@ -11,4 +12,53 @@ use yiiunit\framework\db\ActiveRecordTest;
class PostgreSQLActiveRecordTest extends ActiveRecordTest
{
protected $driverName = 'pgsql';
public function testBooleanValues()
{
$db = $this->getConnection();
$command = $db->createCommand();
$command->batchInsert('bool_values',
['bool_col'], [
[true],
[false],
]
)->execute();
$this->assertEquals(1, BoolAR::find()->where('bool_col = TRUE')->count('*', $db));
$this->assertEquals(1, BoolAR::find()->where('bool_col = FALSE')->count('*', $db));
$this->assertEquals(2, BoolAR::find()->where('bool_col IN (TRUE, FALSE)')->count('*', $db));
$this->assertEquals(1, BoolAR::find()->where(['bool_col' => true])->count('*', $db));
$this->assertEquals(1, BoolAR::find()->where(['bool_col' => false])->count('*', $db));
$this->assertEquals(2, BoolAR::find()->where(['bool_col' => [true, false]])->count('*', $db));
$this->assertEquals(1, BoolAR::find()->where('bool_col = :bool_col', ['bool_col' => true])->count('*', $db));
$this->assertEquals(1, BoolAR::find()->where('bool_col = :bool_col', ['bool_col' => false])->count('*', $db));
$this->assertSame(true, BoolAR::find()->where(['bool_col' => true])->one($db)->bool_col);
$this->assertSame(false, BoolAR::find()->where(['bool_col' => false])->one($db)->bool_col);
}
public function testBooleanDefaultValues()
{
$model = new BoolAR();
$this->assertNull($model->bool_col);
$this->assertNull($model->default_true);
$this->assertNull($model->default_false);
$model->loadDefaultValues();
$this->assertNull($model->bool_col);
$this->assertSame(true, $model->default_true);
$this->assertSame(false, $model->default_false);
$this->assertTrue($model->save(false));
}
}
class BoolAR extends ActiveRecord
{
public static function tableName()
{
return 'bool_values';
}
}
\ No newline at end of file
......@@ -19,4 +19,39 @@ class PostgreSQLCommandTest extends CommandTest
$command = $db->createCommand($sql);
$this->assertEquals('SELECT "id", "t"."name" FROM "customer" t', $command->sql);
}
public function testBooleanValuesInsert()
{
$db = $this->getConnection();
$command = $db->createCommand();
$command->insert('bool_values', ['bool_col' => true]);
$this->assertEquals(1, $command->execute());
$command = $db->createCommand();
$command->insert('bool_values', ['bool_col' => false]);
$this->assertEquals(1, $command->execute());
$command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = TRUE;');
$this->assertEquals(1, $command->queryScalar());
$command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = FALSE;');
$this->assertEquals(1, $command->queryScalar());
}
public function testBooleanValuesBatchInsert()
{
$db = $this->getConnection();
$command = $db->createCommand();
$command->batchInsert('bool_values',
['bool_col'], [
[true],
[false],
]
);
$this->assertEquals(2, $command->execute());
$command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = TRUE;');
$this->assertEquals(1, $command->queryScalar());
$command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = FALSE;');
$this->assertEquals(1, $command->queryScalar());
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@
namespace yiiunit\framework\db\pgsql;
use yii\db\pgsql\Schema;
use yii\db\Query;
use yiiunit\framework\db\QueryTest;
use yiiunit\framework\db\SchemaTest;
......@@ -13,4 +14,27 @@ use yiiunit\framework\db\SchemaTest;
class PostgreSQLQueryTest extends QueryTest
{
public $driverName = 'pgsql';
public function testBooleanValues()
{
$db = $this->getConnection();
$command = $db->createCommand();
$command->batchInsert('bool_values',
['bool_col'], [
[true],
[false],
]
)->execute();
$this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = TRUE')->count('*', $db));
$this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = FALSE')->count('*', $db));
$this->assertEquals(2, (new Query())->from('bool_values')->where('bool_col IN (TRUE, FALSE)')->count('*', $db));
$this->assertEquals(1, (new Query())->from('bool_values')->where(['bool_col' => true])->count('*', $db));
$this->assertEquals(1, (new Query())->from('bool_values')->where(['bool_col' => false])->count('*', $db));
$this->assertEquals(2, (new Query())->from('bool_values')->where(['bool_col' => [true, false]])->count('*', $db));
$this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = :bool_col', ['bool_col' => true])->count('*', $db));
$this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = :bool_col', ['bool_col' => false])->count('*', $db));
}
}
......@@ -26,6 +26,10 @@ class PostgreSQLSchemaTest extends SchemaTest
$columns['int_col2']['size'] = null;
$columns['int_col2']['precision'] = 32;
$columns['int_col2']['scale'] = 0;
$columns['smallint_col']['dbType'] = 'int2';
$columns['smallint_col']['size'] = null;
$columns['smallint_col']['precision'] = 16;
$columns['smallint_col']['scale'] = 0;
$columns['char_col']['dbType'] = 'bpchar';
$columns['char_col']['precision'] = null;
$columns['char_col2']['dbType'] = 'varchar';
......@@ -80,4 +84,14 @@ class PostgreSQLSchemaTest extends SchemaTest
}
fclose($fp);
}
public function testBooleanDefaultValues()
{
/* @var $schema Schema */
$schema = $this->getConnection()->schema;
$table = $schema->getTableSchema('bool_values');
$this->assertSame(true, $table->getColumn('default_true')->defaultValue);
$this->assertSame(false, $table->getColumn('default_false')->defaultValue);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment