/
MetaSeoTest.php
192 lines (167 loc) · 5.89 KB
/
MetaSeoTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?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;
use Illuminate\Support\Str;
/**
* 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;
/**
* @var array[]|null
*/
protected $incidents;
/**
* 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);
$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)],
];
}
/**
* 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 = $this->incidents[0]['description'];
$incident = $this->createIncidentWithMeta($this->incidents[1], ['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 = $this->incidents[0]['title'];
$incident = $this->createIncidentWithMeta($this->incidents[1], ['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($this->incidents[1]);
$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));
$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($this->incidents[1]);
$expectedTitle = sprintf('%s | %s', $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 $incident
* @param array $meta
*
* @return Incident
*/
protected function createIncidentWithMeta(array $incident, array $meta = [])
{
$this->signIn();
$name = $incident['title'];
$message = $incident['description'];
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();
}
}