winter/modules/system/tests/plugins/database/MorphToModelTest.php
Jack Wilkinson 7fe167c41f
Split tests into relevant module folders
This splits the testing suite into the separate modules as appropriate in order to improve the reliability of the testing suite as a whole and make it easier for developers to have an up to date testing suite from the core to build off of. Additionally the tests are now namespaced and some minor improvements to the PluginManager were made.

Now the PluginManager will internally treat plugin identifiers as lower case strings, only transforming them to their normalized versions when requested by methods like getPlugins() & getAllPlugins(). The idea behind this is that it provides a much simpler way to internally handle checking, especially for plugin replacement where casing could cause issues.

Replaces #576. Fixes #575.
2022-06-21 18:30:30 -06:00

67 lines
2.3 KiB
PHP

<?php
namespace System\Tests\Plugins\Database;
use System\Tests\Bootstrap\PluginTestCase;
use Database\Tester\Models\Post;
use Database\Tester\Models\Author;
use Database\Tester\Models\EventLog;
use Model;
class MorphToModelTest extends PluginTestCase
{
public function setUp() : void
{
parent::setUp();
include_once base_path() . '/modules/system/tests/fixtures/plugins/database/tester/models/Author.php';
include_once base_path() . '/modules/system/tests/fixtures/plugins/database/tester/models/Post.php';
include_once base_path() . '/modules/system/tests/fixtures/plugins/database/tester/models/EventLog.php';
$this->runPluginRefreshCommand('Database.Tester');
}
public function testSetRelationValue()
{
Model::unguard();
$author = Author::create(['name' => 'Stevie', 'email' => 'stevie@example.com']);
$post1 = Post::create(['title' => "First post", 'description' => "Yay!!"]);
$post2 = Post::make(['title' => "Second post", 'description' => "Woohoo!!"]);
$event = EventLog::create(['action' => "user-created"]);
Model::reguard();
// Set by Model object
$event->related = $author;
$event->save();
$this->assertEquals($author->id, $event->related_id);
$this->assertEquals('Stevie', $event->related->name);
// Set by primary key
$event->related = [$post1->id, get_class($post1)];
$this->assertEquals($post1->id, $event->related_id);
$this->assertEquals('First post', $event->related->title);
// Nullify
$event->related = null;
$this->assertNull($event->related_id);
$this->assertNull($event->related);
// Deferred in memory
$event->related = $post2;
$this->assertEquals('Second post', $event->related->title);
$this->assertNull($event->related_id);
$event->save();
$this->assertEquals($post2->id, $event->related_id);
}
public function testGetRelationValue()
{
Model::unguard();
$author = Author::create(['name' => 'Stevie']);
$event = EventLog::make(['action' => "user-created", 'related_id' => $author->id, 'related_type' => get_class($author)]);
Model::reguard();
$this->assertEquals([$author->id, get_class($author)], $event->getRelationValue('related'));
}
}