From 8d8d196aac4fea034fcc9f3b3f3cc236f4e87ccc Mon Sep 17 00:00:00 2001 From: Nico Stapelbroek Date: Mon, 19 Mar 2018 09:28:03 +0100 Subject: [PATCH] Add tests for the MetaSeo fields --- app/Bus/Handlers/Traits/StoresMeta.php | 6 +- tests/Functional/Incident/MetaSeoTest.php | 184 ++++++++++++++++++++++ 2 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 tests/Functional/Incident/MetaSeoTest.php diff --git a/app/Bus/Handlers/Traits/StoresMeta.php b/app/Bus/Handlers/Traits/StoresMeta.php index 30eedf8b..0c4a4c2f 100644 --- a/app/Bus/Handlers/Traits/StoresMeta.php +++ b/app/Bus/Handlers/Traits/StoresMeta.php @@ -22,9 +22,9 @@ trait StoresMeta * @param $type * @param $id * - * @return void - * * @throws \Exception + * + * @return void */ public function storeMeta($meta, $type, $id) { @@ -47,7 +47,7 @@ trait StoresMeta } // The value is empty, remove the row - if($meta->exists) { + if ($meta->exists) { $meta->delete(); } } diff --git a/tests/Functional/Incident/MetaSeoTest.php b/tests/Functional/Incident/MetaSeoTest.php new file mode 100644 index 00000000..10d375c6 --- /dev/null +++ b/tests/Functional/Incident/MetaSeoTest.php @@ -0,0 +1,184 @@ + + * @author Graham Campbell + */ +class MetaSeoTest extends AbstractTestCase +{ + use DatabaseMigrations; + + /** + * @var \Faker\Generator + */ + protected $fakerFactory; + + /** + * @var string + */ + protected $appName; + + /** + * CreateIncidentCommandTest constructor. + * + * @param null $name + * @param array $data + */ + public function __construct($name = null, array $data = [], $dataName = '') + { + parent::__construct($name, $data, $dataName); + $this->fakerFactory = \Faker\Factory::create(); + $this->appName = 'MetaSeoTest'; + } + + /** + * Setup the application. + */ + public function setUp() + { + parent::setUp(); + $this->app->make(SettingsRepository::class)->set('app_name', $this->appName); + } + + /** + * When using a custom meta description in an incident it will be + * showed in two meta tags on the incident details page. + */ + public function testCustomSeoDescriptionOnIncidentPage() + { + $description = $this->fakerFactory->sentence; + + $incident = $this->createIncidentWithMeta(['seo' => ['description' => $description]]); + $page = $this->get(sprintf('/incidents/%d', $incident->id))->response; + + $this->assertContains( + sprintf('', $description), + $page->content() + ); + $this->assertContains( + sprintf('', $description), + $page->content() + ); + } + + /** + * When using a custom meta title in an incident it will be + * showed in two meta tags on the incident details page. + */ + public function testCustomSeoTitleOnIncidentPage() + { + $title = $this->fakerFactory->title; + + $incident = $this->createIncidentWithMeta(['seo' => ['title' => $title]]); + $page = $this->get(sprintf('/incidents/%d', $incident->id))->response; + + $this->assertContains( + sprintf('', $title, $this->appName), + $page->content() + ); + $this->assertContains( + sprintf('%s | %s', $title, $this->appName), + $page->content() + ); + } + + /** + * When using no custom meta description in an incident, the application + * default generated description will be used on the incident details page. + */ + public function testNoCustomSeoDescriptionOnIncidentPage() + { + $incident = $this->createIncidentWithMeta([]); + $presenter = $this->app->make(IncidentPresenter::class); + $presenter->setWrappedObject($incident); + + $expectedDescription = sprintf( + 'Details and updates about the %s incident that occurred on %s', + $incident->name, + $presenter->occurred_at_formatted + ); + + $page = $this->get(sprintf('/incidents/%d', $incident->id))->response; + + $this->assertContains( + sprintf('', $expectedDescription), + $page->content() + ); + $this->assertContains( + sprintf('', $expectedDescription), + $page->content() + ); + } + + /** + * When using no custom meta description in an incident, the application + * default generated description will be used on the incident details page. + */ + public function testNoCustomSeoTitleOnIncidentPage() + { + $incident = $this->createIncidentWithMeta([]); + $expectedTitle = sprintf('%s | %s', $incident->name, $this->appName); + + $page = $this->get(sprintf('/incidents/%d', $incident->id))->response; + + $this->assertContains( + sprintf('', $expectedTitle), + $page->content() + ); + $this->assertContains(sprintf('%s', $expectedTitle), $page->content()); + } + + /** + * @param array $meta + * + * @return \Illuminate\Database\Eloquent\Model|static + */ + protected function createIncidentWithMeta(array $meta) + { + $user = factory(User::class)->create(); + $user->save(); + + Auth::login($user); + $name = $this->fakerFactory->name; + $message = $this->fakerFactory->sentence; + + dispatch(new CreateIncidentCommand( + $name, + $this->fakerFactory->numberBetween(0, 3), + $message, + $this->fakerFactory->boolean, + null, + null, + false, + $this->fakerFactory->boolean, + $this->fakerFactory->date('Y-m-d H:i'), + null, + [], + $meta + )); + + return Incident::where('name', '=', $name)->where('message', '=', $message)->firstOrFail(); + } +}