1+ <?php
2+
3+ use Livewire \Livewire ;
4+ use Mockery \MockInterface ;
5+ 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 ;
10+ use SolutionForest \InspireCms \Tests \TestCase ;
11+
12+ uses (TestCase::class);
13+ pest ()->group ('livewire ' , 'feature ' , 'content-version-history ' );
14+
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+
65+ function getLivewireParams ($ content ): array
66+ {
67+ return [
68+ 'ownerRecord ' => $ content ,
69+ 'pageClass ' => 'SolutionForest \\InspireCms \\Filament \\Resources \\ContentResource \\Pages \\EditContentRecord ' ,
70+ ];
71+ }
72+
73+ it ('renders_content_version_history_component ' , function () {
74+ $ content = createContent (
75+ [
76+ 'banner ' => [
77+ 'title ' => 'Test Content Title ' ,
78+ ],
79+ ],
80+ );
81+ Livewire::test ('inspirecms::content-version-history ' , getLivewireParams ($ content ))
82+ ->assertSee (__ ('inspirecms::resources/content-version.tables.search_placeholder ' ));
83+ });
84+
85+ test ('rollback_content_version ' , function () {
86+
87+ // Guard against running this test without a super admin user
88+ $ this ->createSuperAdminUser ();
89+ $ this ->loginCmsPanelAsSuperAdmin ();
90+
91+ $ content = createContent (['banner ' => ['title ' => 'Test Content Title ' ]], ['published_at ' => now ()->subDays (2 )], 'publish ' );
92+
93+ expect ($ content ->contentVersions ()->count ())->toBe (1 );
94+ expect ($ content ->toDto ()->getPropertyValue ('banner ' , 'title ' ))->toBe ('Test Content Title ' );
95+
96+ // Add new version
97+ $ content = addContentVersion ($ content , ['banner ' => ['title ' => 'Test Content Title 2 ' ]], ['published_at ' => now ()], 'publish ' );
98+ expect ($ content ->contentVersions ()->count ())->toBe (2 );
99+ expect ($ content ->toDto ()->getPropertyValue ('banner ' , 'title ' ))->toBe ('Test Content Title 2 ' );
100+
101+ $ contentVersions = $ content ->contentVersions ()->get ();
102+ $ firstVersion = $ contentVersions ->first ();
103+ $ latestVersion = $ contentVersions ->last ();
104+ expect ($ firstVersion )->not ->toBeNull ();
105+ expect ($ latestVersion )->not ->toBeNull ();
106+ expect ($ firstVersion ->id != $ latestVersion ->id )->toBeTrue ();
107+
108+
109+ Livewire::test ('inspirecms::content-version-history ' , getLivewireParams ($ content ))
110+ ->assertCountTableRecords (2 )
111+ ->assertTableActionHidden ('rollbackToVersion ' , $ firstVersion );
112+
113+ $ this ->mock (LicenseManager::class, function (MockInterface $ mock ) {
114+ $ mock ->shouldReceive ('canRollbackVersion ' )->andReturn (true );
115+ });
116+ expect (app (LicenseManager::class)->canRollbackVersion ())->toBe (true );
117+
118+ Livewire::test ('inspirecms::content-version-history ' , getLivewireParams ($ content ))
119+ ->assertTableActionHidden ('rollbackToVersion ' , $ latestVersion )
120+ ->callTableAction ('rollbackToVersion ' , $ firstVersion )
121+ ->assertHasNoTableActionErrors ();
122+
123+ });
0 commit comments