diff --git a/tests/Support/Fixtures/site-trees/default.php b/tests/Support/Fixtures/site-trees/default.php new file mode 100644 index 00000000..62662877 --- /dev/null +++ b/tests/Support/Fixtures/site-trees/default.php @@ -0,0 +1,48 @@ + 'undergraduate', + 'title' => 'Undergraduates', + 'layout' => ['name' => 'test-layout', 'version' => 1], + 'children' => [ + [ + 'slug' => '2017', + 'title' => '2017 Entry', + 'layout' => ['name' => 'test-layout', 'version' => 1] + ], + [ + 'slug' => '2018', + 'title' => '2018 Entry', + 'layout' => ['name' => 'test-layout', 'version' => 1] + ], + ] + ], + [ + 'slug' => 'postgraduate', + 'title' => 'Postgraduates', + 'layout' => ['name' => 'test-layout', 'version' => 1], + 'children' => [ + [ + 'slug' => '2017', + 'title' => '2017 Entry', + 'layout' => ['name' => 'test-layout', 'version' => 1] + ], + [ + 'slug' => '2018', + 'title' => '2018 Entry', + 'layout' => ['name' => 'test-layout', 'version' => 1] + ], + ] + ] +]; diff --git a/tests/Unit/Models/APICommands/APICommandTestCase.php b/tests/Unit/Models/APICommands/APICommandTestCase.php index b4f616d7..6b0e58ea 100644 --- a/tests/Unit/Models/APICommands/APICommandTestCase.php +++ b/tests/Unit/Models/APICommands/APICommandTestCase.php @@ -4,15 +4,25 @@ use App\Models\Contracts\APICommand; use App\Models\LocalAPIClient; +use App\Models\PublishingGroup; use Exception; use Illuminate\Contracts\Auth\Authenticatable; +use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Config; use Tests\TestCase; use Illuminate\Support\Facades\Validator; use App\Models\User; +/** + * Utility methods for testing the API Commands. + * Includes methods for setting up test fixtures. + * @package Tests\Unit\Models\APICommands + */ abstract class APICommandTestCase extends TestCase { + // wraps all database calls within a test in a transaction. + use DatabaseTransactions; + /** * @var array A valid (existing) layout name and version */ @@ -22,15 +32,49 @@ abstract class APICommandTestCase extends TestCase ]; /** + * Should be implemented to return an array of input data which would be valid for the command + * being tested. + * Used by @see APICommandTestCase::input() to get some valid default data to use in a test. * @return array Valid input data. */ abstract public function getValidData(); /** + * Should return an instance of the command class under test. * @return APICommand A new instance of the class to test. */ abstract public function fixture(); + /** + * Create a site with an initial draft page hierarchy to use for testing. + * @param LocalAPIClient $api Used to create the site, etc. + * @param $structure + * @param string $name - The name for the site to add. + * @return Site - The created Site. + */ + public function setupSite(LocalAPIClient $api, $structure, $name) + { + $pubgroup = PublishingGroup::create(['name' => 'test']); + try{ + $site = $api->createSite($pubgroup->id, "Test", "dfgkent.ac.uk", "", [ "name" => "test-layout", "version" => 1]); + }catch(\Exception $e){ + dd($e); + } + $api->addTree($site->homepage->id, null, $structure); + return $site; + } + + /** + * Loads a site structure definition to use with $api->addTree from a php file. + * Assumes that the file returns an array. + * @param string $name - The name of the file without the .php extension. + * @return array - An array defining the site hierarchy. + */ + public function loadSiteStructure($name) + { + return require(dirname(__FILE__) . '/../../../Support/Fixtures/site-trees/' . $name . '.php'); + } + /** * Get an API Client to use. * @param Authenticatable|null $user Optional Authenticatable. If null, will be auto-provided. diff --git a/tests/Unit/Models/APICommands/AddPageTest.php b/tests/Unit/Models/APICommands/AddPageTest.php index 2f45341a..a2b9afad 100644 --- a/tests/Unit/Models/APICommands/AddPageTest.php +++ b/tests/Unit/Models/APICommands/AddPageTest.php @@ -88,7 +88,11 @@ public function validation_whenParentID_IsMissing() */ public function validation_whenParent_existsButIsNotDraft_fails() { - $this->markTestIncomplete(); + $api = $this->api(); + $site = $this->setupSite($api, $this->loadSiteStructure('default'), 'Test'); + $api->publishPage($site->homepage->id); + $api->publishPage($site->homepage->children()->first()->id); + } /**