Tests to prove double save issue fixed

Has one and morph one relations would null on a second save. This is because the relation would use an UPDATE sql query to null the value, then leverage eloquent's save() method to apply it again. Eloquent's save() method does not apply attributes that are unchanged (not dirty), resulting in the value being left as null.

Refs #1986
This commit is contained in:
Samuel Georges 2016-11-22 08:07:10 +11:00
parent b885ca0d24
commit 7b0531e0a0
2 changed files with 59 additions and 0 deletions

View File

@ -30,6 +30,10 @@ class HasOneModelTest extends PluginTestCase
$this->assertEquals($author->id, $phone1->author_id);
$this->assertEquals('0404040404', $author->phone->number);
// Double check
$phone1 = Phone::find($phone1->id);
$this->assertEquals($author->id, $phone1->author_id);
// Set by primary key
$phoneId = $phone2->id;
$author->phone = $phoneId;
@ -38,6 +42,10 @@ class HasOneModelTest extends PluginTestCase
$this->assertEquals($author->id, $phone2->author_id);
$this->assertEquals('0505050505', $author->phone->number);
// Ensure relationship is "stolen" from first model
$phone1 = Phone::find($phone1->id);
$this->assertNotEquals($author->id, $phone1->author_id);
// Nullify
$author->phone = null;
$author->save();
@ -51,6 +59,25 @@ class HasOneModelTest extends PluginTestCase
$this->assertEquals($author->id, $phone3->author_id);
}
public function testSetRelationValueTwice()
{
Model::unguard();
$author = Author::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
$phone = Phone::create(['number' => '0505050505']);
Model::reguard();
$phoneId = $phone->id;
$author->phone = $phoneId;
$author->save();
$author->phone = $phoneId;
$author->save();
$phone = Phone::find($phoneId);
$this->assertEquals($author->id, $phone->author_id);
$this->assertEquals('0505050505', $author->phone->number);
}
public function testGetRelationValue()
{
Model::unguard();

View File

@ -59,6 +59,11 @@ class MorphOneModelTest extends PluginTestCase
$this->assertEquals(get_class($post), $meta1->taggable_type);
$this->assertEquals('Question', $post->meta->meta_title);
// Double check
$meta1 = Meta::find($meta1->id);
$this->assertEquals($post->id, $meta1->taggable_id);
$this->assertEquals(get_class($post), $meta1->taggable_type);
// Set by primary key
$metaId = $meta2->id;
$author->meta = $metaId;
@ -82,6 +87,33 @@ class MorphOneModelTest extends PluginTestCase
$this->assertEquals($author->id, $meta3->taggable_id);
}
public function testSetRelationValueTwice()
{
Model::unguard();
$author = Author::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
$meta = Meta::create([
'meta_title' => 'Question',
'meta_description' => 'Industry',
'meta_keywords' => 'major',
'canonical_url' => 'http://google.com/search/jobs',
'redirect_url' => 'http://google.com',
'robot_index' => 'index',
'robot_follow' => 'follow',
]);
Model::reguard();
$metaId = $meta->id;
$author->meta = $metaId;
$author->save();
$author->meta = $metaId;
$author->save();
$meta = Meta::find($metaId);
$this->assertEquals($author->id, $meta->taggable_id);
$this->assertEquals(get_class($author), $meta->taggable_type);
}
public function testGetRelationValue()
{
Model::unguard();