public function behaviors() { return ['multilingual' => ['class' => MultilingualActiveRecord::className(), 'translationModelClass' => PostTranslation::className()]]; }
public function testRecords() { Yii::$app->trigger(Application::EVENT_BEFORE_REQUEST); $_SERVER['SERVER_NAME'] = 'example.com'; $_SERVER['REQUEST_URI'] = '/en/url/to?something=yes'; $this->resolve(); /** @var Post|PostTranslation|MultilingualActiveRecord $post */ // all published records in eng $posts = Post::find()->all(); $this->assertEquals(3, count($posts)); foreach ($posts as $post) { $this->assertEquals(1, $post->is_published); } // the same as above but without applyDefaultScope in the model $posts = PostNoScope::find()->all(); $this->assertEquals(3, count($posts)); foreach ($posts as $post) { $this->assertEquals(1, $post->is_published); } // all posts including unpublished in eng $posts = AllPost::find()->all(); $this->assertEquals(5, count($posts)); // find post with all translations $post = Post::findOne(1); $this->assertTrue($post->hasTranslation(1)); $this->assertTrue($post->hasTranslation(2)); $this->assertTrue($post->hasTranslation(3)); $this->assertFalse($post->hasTranslation(4)); // find post without published en translation $post = Post::findOne(4); // by-default as we have innerjoin trait the post will not be found as it doen't has en published translation $this->assertNull($post); // same but without published state $post = AllPost::findOne(4); $this->assertNotNull($post); $this->assertTrue($post->hasTranslation(1)); $this->assertFalse($post->hasTranslation(4)); // find post without existing en translation $post = AllPostNoTrait::findOne(6); $this->assertNotNull($post); $this->assertFalse($post->hasTranslation(1)); $this->assertFalse($post->hasTranslation()); $this->assertFalse($post->hasTranslation(2)); $this->assertTrue($post->hasTranslation(3)); $this->assertFalse($post->hasTranslation(4)); /** @var PostTranslation $translation */ $translation = $post->translate(3); $this->assertEquals($translation->is_published, 1); $this->assertEquals($translation->title, 'Post titel 6'); $translation = $post->translate(1); $this->assertTrue($translation->isNewRecord); $this->assertEquals($translation->is_published, 1); $this->assertEquals($translation->title, ''); $this->assertEquals($post->title, ''); $this->assertFalse($post->save()); $this->assertArrayHasKey('translations', $post->errors); $this->assertArrayHasKey('title', $translation->errors); $post->title = 'New post 6 title'; $post->body = 'New post 6 body'; $this->assertEquals($translation->title, 'New post 6 title'); $this->assertEquals($post->title, 'New post 6 title'); $this->assertTrue($post->save()); $this->assertEquals(1, $post->delete()); $this->assertNull(AllPostNoTrait::findOne(6)); $this->assertNull(PostTranslation::findOne(['model_id' => 6, 'language_id' => 3])); // find post with en but without ru and unpublished de $post = Post::findOne(5); $this->assertTrue($post->hasTranslation(1)); $this->assertFalse($post->hasTranslation(2)); $this->assertFalse($post->hasTranslation(3)); $this->assertFalse($post->hasTranslation(4)); $this->assertTrue($post->hasTranslation()); }