22
33namespace Tests \Entries ;
44
5+ use Illuminate \Foundation \Testing \RefreshDatabase ;
6+ use Illuminate \Support \Carbon ;
7+ use Illuminate \Support \Facades \DB ;
58use PHPUnit \Framework \Attributes \Test ;
69use Statamic \Eloquent \Entries \EntryModel ;
710use Tests \TestCase ;
811
912class EntryModelTest extends TestCase
1013{
14+ use RefreshDatabase;
15+
1116 #[Test]
1217 public function it_gets_attributes_from_json_column ()
1318 {
@@ -22,4 +27,88 @@ public function it_gets_attributes_from_json_column()
2227 $ this ->assertEquals ('bar ' , $ model ->foo );
2328 $ this ->assertEquals (['foo ' => 'bar ' ], $ model ->data );
2429 }
30+
31+ #[Test]
32+ public function it_gets_date_as_utc ()
33+ {
34+ config ()->set ('app.timezone ' , 'America/New_York ' ); // -05:00
35+ date_default_timezone_set ('America/New_York ' );
36+
37+ DB ::table ('entries ' )->insert ([
38+ 'id ' => '1 ' ,
39+ 'site ' => 'en ' ,
40+ 'published ' => 1 ,
41+ 'date ' => '2025-01-01 12:11:10 ' ,
42+ 'collection ' => 'blog ' ,
43+ 'data ' => '{} ' ,
44+ ]);
45+
46+ $ date = EntryModel::find (1 )->date ;
47+
48+ $ this ->assertInstanceOf (Carbon::class, $ date );
49+ $ this ->assertEquals ('2025-01-01T12:11:10+00:00 ' , $ date ->toIso8601String ());
50+ }
51+
52+ #[Test]
53+ public function it_sets_utc_date_from_string ()
54+ {
55+ config ()->set ('app.timezone ' , 'America/New_York ' ); // -05:00
56+ date_default_timezone_set ('America/New_York ' );
57+
58+ $ model = new EntryModel ();
59+ $ model ->id = 1 ;
60+ $ model ->site = 'en ' ;
61+ $ model ->published = true ;
62+ $ model ->collection = 'blog ' ;
63+ $ model ->date = '2025-01-01 12:11:10 ' ;
64+ $ model ->data = [];
65+ $ model ->save ();
66+
67+ $ this ->assertDatabaseHas ('entries ' , [
68+ 'id ' => 1 ,
69+ 'date ' => '2025-01-01 12:11:10 ' ,
70+ ]);
71+ }
72+
73+ #[Test]
74+ public function it_sets_utc_date_from_carbon ()
75+ {
76+ config ()->set ('app.timezone ' , 'America/New_York ' ); // -05:00
77+ date_default_timezone_set ('America/New_York ' );
78+
79+ $ model = new EntryModel ();
80+ $ model ->id = 1 ;
81+ $ model ->site = 'en ' ;
82+ $ model ->published = true ;
83+ $ model ->collection = 'blog ' ;
84+ $ model ->date = Carbon::parse ('2025-01-01 12:11:10 ' , 'UTC ' );
85+ $ model ->data = [];
86+ $ model ->save ();
87+
88+ $ this ->assertDatabaseHas ('entries ' , [
89+ 'id ' => 1 ,
90+ 'date ' => '2025-01-01 12:11:10 ' ,
91+ ]);
92+ }
93+
94+ #[Test]
95+ public function it_sets_non_utc_date_from_carbon ()
96+ {
97+ config ()->set ('app.timezone ' , 'America/New_York ' ); // -05:00
98+ date_default_timezone_set ('America/New_York ' );
99+
100+ $ model = new EntryModel ();
101+ $ model ->id = 1 ;
102+ $ model ->site = 'en ' ;
103+ $ model ->published = true ;
104+ $ model ->collection = 'blog ' ;
105+ $ model ->date = Carbon::parse ('2025-01-01 12:11:10 ' );
106+ $ model ->data = [];
107+ $ model ->save ();
108+
109+ $ this ->assertDatabaseHas ('entries ' , [
110+ 'id ' => 1 ,
111+ 'date ' => '2025-01-01 17:11:10 ' ,
112+ ]);
113+ }
25114}
0 commit comments