С версии: 1.1.1
Наследование: extends yii\base\Model
 public function testDeletePost()
 {
     $post = Post::findOne(2);
     $this->assertEquals(1, $post->delete());
     $dataSet = $this->getConnection()->createDataSet(['post', 'postLang']);
     $expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-delete-post-na.xml');
     $this->assertDataSetsEqual($expectedDataSet, $dataSet);
 }
 public function testPostHasTranslation()
 {
     $post = new Post();
     $this->assertFalse($post->hasTranslation());
     $this->assertFalse($post->hasTranslation('en-US'));
     $post = Post::findOne(2);
     $this->assertTrue($post->hasTranslation());
     $this->assertTrue($post->hasTranslation('en-US'));
     $this->assertTrue($post->hasTranslation('de-DE'));
     $this->assertTrue($post->hasTranslation('ru-RU'));
 }
 /**
  * @expectedException yii\base\InvalidConfigException
  */
 public function testInvalidToConfigBehavior2()
 {
     $model = new Post(self::$dtConverter);
     $model->attachBehaviors([['class' => ConverterBehavior::className(), 'type' => 4, 'attributes' => [Post::EVENT_BEFORE_VALIDATE => ['datetime']]]]);
 }
Пример #4
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getPost()
 {
     return $this->hasOne(Post::className(), ['id' => 'post_id']);
 }
 public function testFindPostsRelatedByTagValuesAsArray()
 {
     $data = [];
     $models = Post::find()->with('tags')->relatedByTagValues(['tag 3', 'tag  3', 'tag 4', '', 'tag 5'])->all();
     foreach ($models as $model) {
         $data[] = $model->toArray([], ['tags']);
     }
     $this->assertEquals(require __DIR__ . '/data/test-find-posts-related-by-tag-values.php', $data);
     $data = [];
     $models = Post::find()->with('tags')->relatedByTagValues(['tag-3', 'tag-3', 'tag-4', '', 'tag-5'], 'slug')->all();
     foreach ($models as $model) {
         $data[] = $model->toArray([], ['tags']);
     }
     $this->assertEquals(require __DIR__ . '/data/test-find-posts-related-by-tag-values.php', $data);
 }
 public function testSetStringValue()
 {
     /** @var Post $post */
     $post = Post::findOne(2);
     $post->attachBehavior('target-tags', ['class' => Target::className(), 'targetAttribute' => 'tagNames', 'delimiter' => false]);
     $this->assertEquals(['tag 2', 'tag 3', 'tag 4'], $post->tagNames);
     $post->tagNames = 'test';
     $this->assertEquals(['test'], $post->tagNames);
     $post->tagNames = 'test, test2, test3';
     $this->assertEquals(['test, test2, test3'], $post->tagNames);
 }
 public function testValidatePost()
 {
     /** @var Post $post */
     $post = Post::findOne(2);
     $post->attachBehavior('target-tags', ['class' => Target::className(), 'targetAttribute' => 'tagNames', 'beforeLink' => function ($tag) {
         /** @var Tag $tag */
         $tag->frequency = 'aaa';
     }]);
     $post->tagNames = ['tag 3', 'tag 6', 'mega long tag name'];
     $this->assertEquals(false, $post->save());
     $this->assertEquals(['tagNames' => ['[tag 6] Frequency must be an integer.', '[mega long tag name] Name should contain at most 6 characters.', '[mega long tag name] Frequency must be an integer.']], $post->getErrors());
 }
 public function testLoadTranslateWithoutCurrentTranslate()
 {
     /** @var Post $model */
     $model = Post::find()->with(['postLangs'])->where(['id' => 1])->one();
     $this->assertEquals(array_keys($model->getRelatedRecords()), ['postLangs']);
     $this->assertEquals($model->titleLang, 'title of the first post');
     $this->assertEquals(array_keys($model->getRelatedRecords()), ['postLangs', 'currentTranslate']);
 }
 public function testJoinCurrentTranslateRelation()
 {
     $query = Post::find()->joinWith(['currentTranslate', 'status.currentTranslate'], false);
     $this->assertEquals($query->createCommand()->getRawSql(), "SELECT `post`.* FROM `post` LEFT JOIN `post_lang` ON `post`.`id` = `post_lang`.`post_id` LEFT JOIN `status` ON `post`.`status_id` = `status`.`id` LEFT JOIN `status_lang` ON `status`.`id` = `status_lang`.`status_id` WHERE (`post_lang`.`lang_id`='en') AND (`status_lang`.`lang_id`='en')");
     Yii::$app->language = 'ru-RU';
     $query = Post::find()->joinWith(['currentTranslate', 'status.currentTranslate'], false);
     $this->assertEquals($query->createCommand()->getRawSql(), "SELECT `post`.* FROM `post` LEFT JOIN `post_lang` ON `post`.`id` = `post_lang`.`post_id` LEFT JOIN `status` ON `post`.`status_id` = `status`.`id` LEFT JOIN `status_lang` ON `status`.`id` = `status_lang`.`status_id` WHERE (`post_lang`.`lang_id` IN ('ru', 'en')) AND (`status_lang`.`lang_id` IN ('ru', 'en'))");
 }
 public function testCheckIsSet()
 {
     $model = new Post();
     $this->assertFalse(isset($model->id));
     $this->assertFalse(isset($model->titleLang));
     $this->assertFalse(isset($model->description));
     $this->assertFalse(isset($model->lang_id));
     $this->assertTrue(isset($model->language));
     $this->assertTrue(isset($model->translation));
     $this->assertTrue(isset($model->currentTranslate));
     $this->assertTrue(isset($model->hasTranslate));
     $this->assertTrue(isset($model->modelTestProperty));
     /** @var Post $model */
     $model = Post::findOne(1);
     $this->assertTrue(isset($model->id));
     $this->assertTrue(isset($model->titleLang));
     $this->assertTrue(isset($model->description));
     $this->assertFalse(isset($model->title));
     $this->assertFalse(isset($model->modelData));
     $model->modelData = 'OK';
     $this->assertTrue(isset($model->modelData));
     $this->assertFalse(isset($model->data));
     $model->data = 'OK';
     $this->assertTrue(isset($model->data));
 }