Fixed flakey meta seo tests (#3587)

This commit is contained in:
Graham Campbell
2019-05-12 23:48:06 +01:00
committed by GitHub
parent 7dd96f0100
commit 5874df28aa

View File

@@ -17,6 +17,7 @@ use CachetHQ\Cachet\Presenters\IncidentPresenter;
use CachetHQ\Cachet\Settings\Repository as SettingsRepository; use CachetHQ\Cachet\Settings\Repository as SettingsRepository;
use CachetHQ\Tests\Cachet\AbstractTestCase; use CachetHQ\Tests\Cachet\AbstractTestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Str;
/** /**
* This is the create incident command test class. * This is the create incident command test class.
@@ -38,6 +39,11 @@ class MetaSeoTest extends AbstractTestCase
*/ */
protected $appName; protected $appName;
/**
* @var array[]|null
*/
protected $incidents;
/** /**
* CreateIncidentCommandTest constructor. * CreateIncidentCommandTest constructor.
* *
@@ -60,6 +66,10 @@ class MetaSeoTest extends AbstractTestCase
parent::setUp(); parent::setUp();
$this->app->make(SettingsRepository::class)->set('app_name', $this->appName); $this->app->make(SettingsRepository::class)->set('app_name', $this->appName);
$this->app->config->set('setting.app_name', $this->appName); $this->app->config->set('setting.app_name', $this->appName);
$this->incidents = [
['title' => 'Foo '.Str::random(16), 'description' => 'Foo Bar Baz '.Str::random(32)],
['title' => 'Foe '.Str::random(16), 'description' => 'Foe Baz Bar '.Str::random(32)],
];
} }
/** /**
@@ -68,9 +78,9 @@ class MetaSeoTest extends AbstractTestCase
*/ */
public function testCustomSeoDescriptionOnIncidentPage() public function testCustomSeoDescriptionOnIncidentPage()
{ {
$expectedDescription = htmlspecialchars($this->fakerFactory->sentence); $expectedDescription = $this->incidents[0]['description'];
$incident = $this->createIncidentWithMeta(['seo' => ['description' => $expectedDescription]]); $incident = $this->createIncidentWithMeta($this->incidents[1], ['seo' => ['description' => $expectedDescription]]);
$page = $this->get(sprintf('/incidents/%d', $incident->id)); $page = $this->get(sprintf('/incidents/%d', $incident->id));
$this->assertContains( $this->assertContains(
@@ -89,9 +99,9 @@ class MetaSeoTest extends AbstractTestCase
*/ */
public function testCustomSeoTitleOnIncidentPage() public function testCustomSeoTitleOnIncidentPage()
{ {
$title = htmlspecialchars($this->fakerFactory->title); $title = $this->incidents[0]['title'];
$incident = $this->createIncidentWithMeta(['seo' => ['title' => $title]]); $incident = $this->createIncidentWithMeta($this->incidents[1], ['seo' => ['title' => $title]]);
$page = $this->get(sprintf('/incidents/%d', $incident->id)); $page = $this->get(sprintf('/incidents/%d', $incident->id));
$this->assertContains( $this->assertContains(
@@ -110,13 +120,13 @@ class MetaSeoTest extends AbstractTestCase
*/ */
public function testNoCustomSeoDescriptionOnIncidentPage() public function testNoCustomSeoDescriptionOnIncidentPage()
{ {
$incident = $this->createIncidentWithMeta([]); $incident = $this->createIncidentWithMeta($this->incidents[1]);
$presenter = $this->app->make(IncidentPresenter::class); $presenter = $this->app->make(IncidentPresenter::class);
$presenter->setWrappedObject($incident); $presenter->setWrappedObject($incident);
$expectedDescription = sprintf( $expectedDescription = sprintf(
'Details and updates about the %s incident that occurred on %s', 'Details and updates about the %s incident that occurred on %s',
htmlspecialchars($incident->name), $incident->name,
$presenter->occurred_at_formatted $presenter->occurred_at_formatted
); );
@@ -138,8 +148,8 @@ class MetaSeoTest extends AbstractTestCase
*/ */
public function testNoCustomSeoTitleOnIncidentPage() public function testNoCustomSeoTitleOnIncidentPage()
{ {
$incident = $this->createIncidentWithMeta([]); $incident = $this->createIncidentWithMeta($this->incidents[1]);
$expectedTitle = sprintf('%s | %s', htmlspecialchars($incident->name), $this->appName); $expectedTitle = sprintf('%s | %s', $incident->name, $this->appName);
$page = $this->get(sprintf('/incidents/%d', $incident->id)); $page = $this->get(sprintf('/incidents/%d', $incident->id));
@@ -151,15 +161,16 @@ class MetaSeoTest extends AbstractTestCase
} }
/** /**
* @param array $incident
* @param array $meta * @param array $meta
* *
* @return Incident * @return Incident
*/ */
protected function createIncidentWithMeta(array $meta) protected function createIncidentWithMeta(array $incident, array $meta = [])
{ {
$this->signIn(); $this->signIn();
$name = $this->fakerFactory->name; $name = $incident['title'];
$message = $this->fakerFactory->sentence; $message = $incident['description'];
dispatch(new CreateIncidentCommand( dispatch(new CreateIncidentCommand(
$name, $name,