Skip to content

Commit 46a0717

Browse files
committed
add test cases for content invalid publish state
1 parent 99741fd commit 46a0717

File tree

4 files changed

+218
-72
lines changed

4 files changed

+218
-72
lines changed

tests/src/Feature/Livewire/ContentVersionHistoryTest.php

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,11 @@
33
use Livewire\Livewire;
44
use Mockery\MockInterface;
55
use SolutionForest\InspireCms\Licensing\LicenseManager;
6-
use SolutionForest\InspireCms\Tests\Models\Content;
7-
use SolutionForest\InspireCms\Tests\Models\DocumentType;
8-
use SolutionForest\InspireCms\Tests\Models\Field;
9-
use SolutionForest\InspireCms\Tests\Models\FieldGroup;
106
use SolutionForest\InspireCms\Tests\TestCase;
117

128
uses(TestCase::class);
139
pest()->group('livewire', 'feature', 'content-version-history');
1410

15-
function createContent(array $data = [], array $publishableData = [], ?string $publishState = 'publish'): Content
16-
{
17-
$facDocumentType = DocumentType::factory(['category' => 'web']);
18-
19-
foreach ($data as $key => $value) {
20-
if (! is_array($value)) {
21-
continue;
22-
}
23-
$facFieldGroup = FieldGroup::factory(['name' => $key]);
24-
foreach (array_keys($value) as $fieldKey) {
25-
$facFieldGroup = $facFieldGroup->has(Field::factory(['name' => $fieldKey, 'type' => 'text']));
26-
}
27-
$facDocumentType = $facDocumentType->has($facFieldGroup);
28-
}
29-
30-
$facContent = Content::factory()->for($facDocumentType);
31-
if (! empty($data)) {
32-
$facContent = $facContent->havePropertyData($data);
33-
}
34-
35-
$content = $facContent->make();
36-
if ($publishState) {
37-
$content->setPublishableState($publishState);
38-
}
39-
if (! empty($publishableData)) {
40-
$content->setPublishableData($publishableData);
41-
}
42-
$content->save();
43-
$content->refresh();
44-
45-
return $content;
46-
}
47-
48-
function addContentVersion(Content $content, array $data = [], array $publishableData = [], ?string $publishState = 'publish'): Content
49-
{
50-
if (! empty($data)) {
51-
$content->propertyData = json_encode($data);
52-
}
53-
if ($publishState) {
54-
$content->setPublishableState($publishState);
55-
}
56-
if (! empty($publishableData)) {
57-
$content->setPublishableData($publishableData);
58-
}
59-
$content->save();
60-
$content->refresh();
61-
62-
return $content;
63-
}
64-
6511
function getLivewireParams($content): array
6612
{
6713
return [
@@ -71,12 +17,13 @@ function getLivewireParams($content): array
7117
}
7218

7319
it('renders_content_version_history_component', function () {
74-
$content = createContent(
75-
[
20+
$content = $this->createCmsContent(
21+
propData: [
7622
'banner' => [
7723
'title' => 'Test Content Title',
7824
],
7925
],
26+
publishState: 'publish'
8027
);
8128
Livewire::test('inspirecms::content-version-history', getLivewireParams($content))
8229
->assertSee(__('inspirecms::resources/content-version.tables.search_placeholder'));
@@ -88,13 +35,17 @@ function getLivewireParams($content): array
8835
$this->createSuperAdminUser();
8936
$this->loginCmsPanelAsSuperAdmin();
9037

91-
$content = createContent(['banner' => ['title' => 'Test Content Title']], ['published_at' => now()->subDays(2)], 'publish');
38+
$content = $this->createCmsContent(
39+
propData: ['banner' => ['title' => 'Test Content Title']],
40+
publishableData: ['published_at' => now()->subDays(2)],
41+
publishState: 'publish'
42+
);
9243

9344
expect($content->contentVersions()->count())->toBe(1);
9445
expect($content->toDto()->getPropertyValue('banner', 'title'))->toBe('Test Content Title');
9546

9647
// Add new version
97-
$content = addContentVersion($content, ['banner' => ['title' => 'Test Content Title 2']], ['published_at' => now()], 'publish');
48+
$content = $this->addCmsContentVersion($content, ['banner' => ['title' => 'Test Content Title 2']], ['published_at' => now()], 'publish');
9849
expect($content->contentVersions()->count())->toBe(2);
9950
expect($content->toDto()->getPropertyValue('banner', 'title'))->toBe('Test Content Title 2');
10051

tests/src/Feature/PageTemplateTest.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
<?php
22

33
use SolutionForest\InspireCms\Facades\ContentStatusManifest;
4-
use SolutionForest\InspireCms\Helpers\TemplateHelper;
54
use SolutionForest\InspireCms\Tests\Models\Content;
65
use SolutionForest\InspireCms\Tests\Models\DocumentType;
76
use SolutionForest\InspireCms\Tests\Models\Field;
87
use SolutionForest\InspireCms\Tests\Models\FieldGroup;
9-
use SolutionForest\InspireCms\Tests\Models\KeyValue;
10-
use SolutionForest\InspireCms\Tests\Models\Language;
118
use SolutionForest\InspireCms\Tests\Models\Template;
129
use SolutionForest\InspireCms\Tests\TestCase;
1310

@@ -16,20 +13,13 @@
1613

1714
beforeEach(function () {
1815
// Add routes
19-
\SolutionForest\InspireCms\Facades\InspireCms::routes();
16+
$this->registerCmsRoutes();
2017

2118
// Ensure the default language is set
22-
$defaultLangCode = 'en';
23-
Language::updateOrCreate(
24-
['code' => $defaultLangCode],
25-
['is_default' => true]
26-
);
19+
$this->ensureDefaultLanguage();
20+
2721
// Ensure the theme is set up correctly
28-
$this->theme = 'default'; // Set the theme to default
29-
KeyValue::updateOrCreate(
30-
['key' => TemplateHelper::getCurrentThemeKey()],
31-
['value' => $this->theme]
32-
);
22+
$this->theme = $this->ensureDefaultTheme()->value;
3323
});
3424

3525
it('renders_page_template_correctly', function () {

tests/src/TestCase.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Filament\Tables\TablesServiceProvider;
1414
use Filament\Widgets\WidgetsServiceProvider;
1515
use Illuminate\Database\Eloquent\Factories\Factory;
16+
use Illuminate\Support\Facades\Route;
1617
use Khatabwedaa\BladeCssIcons\BladeCssIconsServiceProvider;
1718
use Livewire\LivewireServiceProvider;
1819
use Orchestra\Testbench\TestCase as Orchestra;
@@ -21,10 +22,18 @@
2122
use SolutionForest\InspireCms\Facades\ModelManifest;
2223
use SolutionForest\InspireCms\Helpers\AuthHelper;
2324
use SolutionForest\InspireCms\Helpers\PermissionHelper;
25+
use SolutionForest\InspireCms\Helpers\TemplateHelper;
2426
use SolutionForest\InspireCms\InspireCmsConfig;
2527
use SolutionForest\InspireCms\InspireCmsServiceProvider;
2628
use SolutionForest\InspireCms\Support\Facades\ModelRegistry;
2729
use SolutionForest\InspireCms\Support\InspireCmsSupportServiceProvider;
30+
use SolutionForest\InspireCms\Tests\Models\Content;
31+
use SolutionForest\InspireCms\Tests\Models\DocumentType;
32+
use SolutionForest\InspireCms\Tests\Models\Field;
33+
use SolutionForest\InspireCms\Tests\Models\FieldGroup;
34+
use SolutionForest\InspireCms\Tests\Models\KeyValue;
35+
use SolutionForest\InspireCms\Tests\Models\Language;
36+
use SolutionForest\InspireCms\Tests\Models\Template;
2837
use SolutionForest\InspireCms\Tests\Models\User;
2938

3039
abstract class TestCase extends Orchestra
@@ -221,4 +230,91 @@ public function loginCmsPanelAsSuperAdmin()
221230

222231
return $this->actingAs($user, AuthHelper::guardName());
223232
}
233+
234+
public function registerCmsRoutes()
235+
{
236+
inspirecms()->routes();
237+
238+
return $this;
239+
}
240+
241+
public function ensureDefaultTheme()
242+
{
243+
return KeyValue::updateOrCreate(
244+
['key' => TemplateHelper::getCurrentThemeKey()],
245+
['value' => 'default']
246+
);
247+
}
248+
249+
public function ensureDefaultLanguage()
250+
{
251+
$defaultLangCode = 'en';
252+
253+
return Language::updateOrCreate(
254+
['code' => $defaultLangCode],
255+
['is_default' => true]
256+
);
257+
}
258+
259+
public function addCmsContentVersion($content, array $data = [], array $publishableData = [], ?string $publishState = 'publish')
260+
{
261+
if (! empty($data)) {
262+
$content->propertyData = json_encode($data);
263+
}
264+
if ($publishState) {
265+
$content->setPublishableState($publishState);
266+
}
267+
if (! empty($publishableData)) {
268+
$content->setPublishableData($publishableData);
269+
}
270+
$content->save();
271+
$content->refresh();
272+
273+
return $content;
274+
}
275+
276+
public function createCmsContent(array $data = [], array $propData = [], array $publishableData = [], ?string $publishState = 'draft')
277+
{
278+
$facDocumentType = DocumentType::factory(['category' => 'web'])
279+
->hasAttached(
280+
Template::factory([
281+
'content' => [
282+
$this->ensureDefaultTheme()->value => <<<'HTML'
283+
<div class="content">
284+
<p>Test</p>
285+
</div>
286+
HTML,
287+
],
288+
]),
289+
['is_default' => true]
290+
);
291+
292+
foreach ($propData as $key => $value) {
293+
if (! is_array($value)) {
294+
continue;
295+
}
296+
$facFieldGroup = FieldGroup::factory(['name' => $key]);
297+
foreach (array_keys($value) as $fieldKey) {
298+
$facFieldGroup = $facFieldGroup->has(Field::factory(['name' => $fieldKey, 'type' => 'text']));
299+
}
300+
$facDocumentType = $facDocumentType->has($facFieldGroup);
301+
}
302+
303+
$facContent = Content::factory($data)->for($facDocumentType);
304+
if (! empty($propData)) {
305+
$facContent = $facContent->havePropertyData($propData);
306+
}
307+
308+
$content = $facContent->make();
309+
if ($publishState) {
310+
$content->setPublishableState($publishState);
311+
}
312+
if (! empty($publishableData)) {
313+
$content->setPublishableData($publishableData);
314+
}
315+
$content->save();
316+
$content->refresh();
317+
318+
return $content;
319+
}
224320
}

tests/src/Unit/Models/ContentTest.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,22 @@
66
use SolutionForest\InspireCms\Tests\Models\DocumentType;
77
use SolutionForest\InspireCms\Tests\TestCase;
88

9+
use function PHPUnit\Framework\assertFalse;
10+
use function PHPUnit\Framework\assertNotNull;
11+
use function PHPUnit\Framework\assertNull;
12+
use function PHPUnit\Framework\assertSame;
13+
use function PHPUnit\Framework\assertTrue;
14+
915
uses(TestCase::class);
1016
uses(RefreshDatabase::class);
1117
pest()->group('unit', 'model');
1218

19+
beforeEach(function () {
20+
$this->registerCmsRoutes();
21+
$this->ensureDefaultTheme();
22+
$this->ensureDefaultLanguage();
23+
});
24+
1325
it('creates content version and nestable tree after content creation', function () {
1426
// Act
1527
$content = Content::factory()->havePropertyData([
@@ -116,3 +128,100 @@
116128
$this->assertDatabaseMissing('content_versions', ['content_id' => $content->id]);
117129
$this->assertDatabaseMissing('nestable_trees', ['content_id' => $content->id]);
118130
});
131+
132+
test('content is unpublished when latest version is not published', function () {
133+
$publishableData = [
134+
'published_at' => now()->subDays(2), // Just ensure the content is published initially
135+
];
136+
137+
$parent = $this->createCmsContent(data: [
138+
'slug' => 'home',
139+
'parent_id' => null,
140+
'is_default' => true,
141+
], publishState: 'publish', publishableData: $publishableData);
142+
$contentData = [
143+
'slug' => 'test',
144+
'parent_id' => $parent->getKey(),
145+
'is_default' => false,
146+
];
147+
148+
$validateContent = function (Content $content, bool $validatePublished) use ($contentData) {
149+
$contentService = app(\SolutionForest\InspireCms\Services\ContentServiceInterface::class);
150+
$contentUri = "/{$contentData['slug']}";
151+
$contentParentPath = 'home';
152+
$contentRealPath = "{$contentParentPath}/{$contentData['slug']}";
153+
$strDocumentTypeSlug = $content->documentType?->slug ?? 'default';
154+
155+
if ($validatePublished) {
156+
assertTrue($content->isPublished(), 'Content should be published.');
157+
assertNotNull($content->getPublishTime(), 'Content should have a publish time.');
158+
assertSame(
159+
1,
160+
$contentService->findByIds(ids: $content->getKey(), isPublished: true)->count(),
161+
'Content should be published and found by service.'
162+
);
163+
assertSame(
164+
1,
165+
$contentService->findByRoutePatternWithLangId(uri: $contentUri, isDefaultRoutePattern: true, isPublished: true)->count(),
166+
'Content should be found by route pattern.'
167+
);
168+
assertSame(
169+
1,
170+
$contentService->findByRealPath(path: $contentRealPath, isPublished: true)->count(),
171+
'Content should be found by real path.'
172+
);
173+
assertSame(
174+
1,
175+
$contentService->getUnderRealPath(path: $contentParentPath, isPublished: true)->count(),
176+
'Content should be found under real path.'
177+
);
178+
assertSame(
179+
1,
180+
$contentService->getByDocumentType(documentType: $strDocumentTypeSlug, isPublished: true)->count(),
181+
'Content should be found by document type.'
182+
);
183+
$this->get($content->getUrl())->assertOk();
184+
} else {
185+
assertFalse($content->isPublished(), 'Content should not be published.');
186+
assertNull($content->getPublishTime(), 'Content should not have a publish time.');
187+
assertSame(
188+
0,
189+
$contentService->findByIds(ids: $content->getKey(), isPublished: true)->count(),
190+
'Content should not be published and not found by service.'
191+
);
192+
assertSame(
193+
0,
194+
$contentService->findByRoutePatternWithLangId(uri: $contentUri, isDefaultRoutePattern: true, isPublished: true)->count(),
195+
'Content should not be found by route pattern.'
196+
);
197+
assertSame(
198+
0,
199+
$contentService->findByRealPath(path: $contentRealPath, isPublished: true)->count(),
200+
'Content should not be found by real path.'
201+
);
202+
assertSame(
203+
0,
204+
$contentService->getUnderRealPath(path: $contentParentPath, isPublished: true)->count(),
205+
'Content should not be found under real path.'
206+
);
207+
assertSame(
208+
0,
209+
$contentService->getByDocumentType(documentType: $strDocumentTypeSlug, isPublished: true)->count(),
210+
'Content should not be found by document type.'
211+
);
212+
$this->get($content->getUrl())->assertNotFound();
213+
}
214+
};
215+
216+
$content = $this->createCmsContent(data: $contentData, publishState: 'publish', publishableData: $publishableData);
217+
$validateContent($content, true);
218+
219+
$content = $this->addCmsContentVersion(content: $content, publishState: 'unpublish');
220+
$validateContent($content, false);
221+
222+
$content = $this->addCmsContentVersion(content: $content, publishState: 'draft');
223+
$validateContent($content, false);
224+
225+
$content = $this->addCmsContentVersion(content: $content, publishState: 'publish', publishableData: $publishableData);
226+
$validateContent($content, true);
227+
});

0 commit comments

Comments
 (0)