Merge pull request #2931 from nstapelbroek/feature/2895-custom-meta-descriptions-per-incident

Custom meta descriptions per incident
This commit is contained in:
James Brooks
2019-01-27 14:31:34 +00:00
committed by GitHub
13 changed files with 447 additions and 15 deletions

View File

@@ -0,0 +1,133 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Bus\Traits;
use CachetHQ\Cachet\Bus\Handlers\Traits\StoresMeta;
use CachetHQ\Cachet\Models\Meta;
use CachetHQ\Tests\Cachet\AbstractTestCase;
use Mockery;
use Mockery\MockInterface;
class StoresMetaTest extends AbstractTestCase
{
/**
* @var Meta|MockInterface
*/
protected $metaModel;
/**
* Set up the testcase by mocking the Meta::class.
*/
public function setUp()
{
parent::setUp();
if (!class_exists('Mockery')) {
$this->markTestSkipped('This test requires Mockery');
}
$this->metaModel = Mockery::mock(Meta::class)->makePartial();
$this->app->instance(Meta::class, $this->metaModel);
}
/**
* Our Mockery expectations should count as assertions to prevent warnings from PHPUnit.
*/
public function tearDown()
{
$this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount());
parent::tearDown();
}
/**
* Each array value passed to the MetaValues should result in a new model instance.
*/
public function testStoresMetaWithSimpleMultipleArrays()
{
$mock = $this->getMockForTrait(StoresMeta::class);
$metaData = [
'somekey1' => 'somevalue',
'somekey2' => 'somevalue',
'somekey3' => 'somevalue',
];
$this->metaModel->shouldReceive('firstOrNew')->times(3)->andReturn($this->metaModel);
$this->metaModel->shouldReceive('save')->times(3);
$this->metaModel->shouldReceive('setAttribute')->times(3)->andReturnUsing(function ($key, $value) {
$this->assertEquals('value', $key);
$this->assertEquals('somevalue', $value);
return $this->metaModel;
});
$mock->storeMeta($metaData, 'some_class', 1, $this->metaModel);
}
/**
* It will pass nested arrays to the value property of the Meta model.
*/
public function testStoresNestedArraysAsSingleValue()
{
$mock = $this->getMockForTrait(StoresMeta::class);
$metaData = ['somekey1' => ['subkey' => ['second' => 'key']]];
$this->metaModel->shouldReceive('firstOrNew')->once()->andReturn($this->metaModel);
$this->metaModel->shouldReceive('save')->once();
$this->metaModel->shouldReceive('setAttribute')->once()->andReturnUsing(function ($key, $value) {
$this->assertEquals('value', $key);
$this->assertEquals(['subkey' => ['second' => 'key']], $value);
return $this->metaModel;
});
$mock->storeMeta($metaData, 'some_class', 1, $this->metaModel);
}
/**
* If a value is empty or null it will be removed.
*/
public function testEmptyValuesWillBeDeleted()
{
$mock = $this->getMockForTrait(StoresMeta::class);
$metaData = ['somekey1' => ''];
$this->metaModel->exists = true;
$this->metaModel->shouldReceive('firstOrNew')->once()->andReturn($this->metaModel);
$this->metaModel->shouldReceive('delete')->once();
$this->metaModel->shouldReceive('setAttribute')->never();
$this->metaModel->shouldReceive('save')->never();
$mock->storeMeta($metaData, 'some_class', 1, $this->metaModel);
}
/**
* If a value is empty or null in a nested array it will be removed.
*/
public function testEmptyNestedArrayKeysAreRemoved()
{
$mock = $this->getMockForTrait(StoresMeta::class);
$metaData = ['somekey1' => ['keyWithValue' => 'value123', 'keyWithoutValue' => null]];
$this->metaModel->exists = true;
$this->metaModel->shouldReceive('firstOrNew')->once()->andReturn($this->metaModel);
$this->metaModel->shouldReceive('setAttribute')->once()->andReturnUsing(function ($key, $value) {
$this->assertEquals('value', $key);
$this->assertEquals(['keyWithValue' => 'value123'], $value);
return $this->metaModel;
});
$this->metaModel->shouldReceive('save')->once();
$this->metaModel->shouldReceive('delete')->never();
$mock->storeMeta($metaData, 'some_class', 1, $this->metaModel);
}
}

View File

@@ -0,0 +1,181 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Functional\Bus\Commands\Incident;
use CachetHQ\Cachet\Bus\Commands\Incident\CreateIncidentCommand;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Presenters\IncidentPresenter;
use CachetHQ\Cachet\Settings\Repository as SettingsRepository;
use CachetHQ\Tests\Cachet\AbstractTestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
/**
* This is the create incident command test class.
*
* @author James Brooks <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
*/
class MetaSeoTest extends AbstractTestCase
{
use DatabaseMigrations;
/**
* @var \Faker\Generator
*/
protected $fakerFactory;
/**
* @var string
*/
protected $appName;
/**
* CreateIncidentCommandTest constructor.
*
* @param null $name
* @param array $data
* @param string $dataName
*/
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);
$this->app->config->set('setting.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()
{
$expectedDescription = htmlspecialchars($this->fakerFactory->sentence);
$incident = $this->createIncidentWithMeta(['seo' => ['description' => $expectedDescription]]);
$page = $this->get(sprintf('/incidents/%d', $incident->id));
$this->assertContains(
sprintf('<meta property="og:description" content="%s">', $expectedDescription),
$page->content()
);
$this->assertContains(
sprintf('<meta name="description" content="%s">', $expectedDescription),
$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 = htmlspecialchars($this->fakerFactory->title);
$incident = $this->createIncidentWithMeta(['seo' => ['title' => $title]]);
$page = $this->get(sprintf('/incidents/%d', $incident->id));
$this->assertContains(
sprintf('<meta property="og:title" content="%s | %s">', $title, $this->appName),
$page->content()
);
$this->assertContains(
sprintf('<title>%s | %s</title>', $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',
htmlspecialchars($incident->name),
$presenter->occurred_at_formatted
);
$page = $this->get(sprintf('/incidents/%d', $incident->id));
$this->assertContains(
sprintf('<meta property="og:description" content="%s">', $expectedDescription),
$page->content()
);
$this->assertContains(
sprintf('<meta name="description" content="%s">', $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', htmlspecialchars($incident->name), $this->appName);
$page = $this->get(sprintf('/incidents/%d', $incident->id));
$this->assertContains(
sprintf('<meta property="og:title" content="%s">', $expectedTitle),
$page->content()
);
$this->assertContains(sprintf('<title>%s</title>', $expectedTitle), $page->content());
}
/**
* @param array $meta
*
* @return Incident
*/
protected function createIncidentWithMeta(array $meta)
{
$this->signIn();
$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();
}
}