-
-
Notifications
You must be signed in to change notification settings - Fork 641
Expand file tree
/
Copy pathFeatureTest.php
More file actions
333 lines (282 loc) · 10.4 KB
/
Copy pathFeatureTest.php
File metadata and controls
333 lines (282 loc) · 10.4 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
namespace Tests\Stache;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\AssetContainer;
use Statamic\Facades\Collection;
use Statamic\Facades\Data;
use Statamic\Facades\Entry;
use Statamic\Facades\File;
use Statamic\Facades\GlobalSet;
use Statamic\Facades\Nav;
use Statamic\Facades\Nav as NavRepository;
use Statamic\Facades\Structure;
use Statamic\Facades\Taxonomy;
use Statamic\Facades\User;
use Statamic\Stache\Stache;
use Tests\TestCase;
class FeatureTest extends TestCase
{
private $stache;
public function setUp(): void
{
parent::setUp();
// Use the file driver which is a more accurate representation of how the Stache would be used.
config(['cache.default' => 'file']);
$this->artisan('cache:clear');
$this->stache = tap($this->app->make('stache'), function ($stache) {
$dir = __DIR__.'/__fixtures__';
$stache->store('taxonomies')->directory($dir.'/content/taxonomies');
$stache->store('collections')->directory($dir.'/content/collections');
$stache->store('entries')->directory($dir.'/content/collections');
$stache->store('navigation')->directory($dir.'/content/navigation');
$stache->store('globals')->directory($dir.'/content/globals');
$stache->store('global-variables')->directory($dir.'/content/globals');
$stache->store('asset-containers')->directory($dir.'/content/assets');
$stache->store('collection-trees')->directory($dir.'/content/structures/collections');
$stache->store('nav-trees')->directory($dir.'/content/structures/navigation');
$stache->store('users')->directory($dir.'/users');
$stache->store('form-submissions')->directory($dir.'/content/submissions');
});
}
#[Test]
public function it_gets_all_collections()
{
$this->assertEquals(5, Collection::all()->count());
}
#[Test]
public function it_gets_all_entries()
{
$this->assertEquals(14, Entry::all()->count());
$this->assertEquals(3, Entry::whereCollection('alphabetical')->count());
$this->assertEquals(2, Entry::whereCollection('blog')->count());
$this->assertEquals(3, Entry::whereCollection('numeric')->count());
$this->assertEquals(6, Entry::whereCollection('pages')->count());
$this->assertEquals(5, Entry::whereInCollection(['alphabetical', 'blog'])->count());
}
#[Test]
public function it_gets_entry()
{
$entry = Entry::find('blog-christmas');
$this->assertEquals('Christmas', $entry->get('title'));
$this->assertEquals($entry, Data::find('entry::blog-christmas'));
$this->assertEquals($entry, Data::find('blog-christmas'));
// ensure it only gets from the entries' store, not anywhere in the stache.
$this->assertNull(Entry::find('users-john'));
}
#[Test]
public function it_gets_all_taxonomies()
{
$this->assertEquals(2, Taxonomy::all()->count());
}
#[Test]
public function it_gets_all_globals()
{
$this->assertEquals(2, GlobalSet::all()->count());
}
#[Test]
public function it_gets_globals()
{
$global = GlobalSet::find('global');
$this->assertEquals('Bar', $global->in('en')->get('foo'));
$this->assertEquals($global, Data::find('global::global'));
$this->assertEquals($global, Data::find('global'));
$this->assertEquals('+1 555-1234', GlobalSet::find('contact')->in('en')->get('phone'));
}
#[Test]
public function it_gets_asset_containers()
{
$this->assertEquals(2, AssetContainer::all()->count());
}
#[Test]
public function it_gets_an_asset_container()
{
$this->assertEquals('Main Assets', AssetContainer::find('main')->title());
$this->assertEquals('Another Asset Container', AssetContainer::find('another')->title());
}
#[Test]
public function it_gets_users()
{
$this->assertEquals(2, User::all()->count());
}
#[Test]
public function it_gets_a_user()
{
$user = User::find('users-john');
$this->assertEquals('users-john', $user->id());
$this->assertEquals('John Smith', $user->get('name'));
$this->assertEquals('john@example.com', $user->email());
$this->assertEquals($user, Data::find('user::users-john'));
$this->assertEquals($user, Data::find('users-john'));
}
#[Test]
public function it_gets_an_entry_by_uri()
{
$entry = Entry::findByUri('/numeric/two');
$this->assertInstanceOf(\Statamic\Contracts\Entries\Entry::class, $entry);
$this->assertEquals('numeric-two', $entry->id());
$this->assertEquals('Two', $entry->get('title'));
$this->assertNull(Entry::findByUri('/unknown'));
}
#[Test]
public function it_gets_an_entry_in_structure_by_uri()
{
$entry = Entry::findByUri('/about/board/directors');
$this->assertInstanceOf(\Statamic\Structures\Page::class, $entry);
$this->assertEquals('pages-directors', $entry->id());
$this->assertEquals('Directors', $entry->title());
}
#[Test]
public function it_returns_null_when_cannot_find_entry_by_uri()
{
$this->assertNull(Entry::findByUri('/unknown'));
}
#[Test]
public function it_gets_structures()
{
$this->assertEquals(3, Structure::all()->count());
}
#[Test]
public function it_gets_a_structure()
{
$structure = Structure::find('footer');
$this->assertEquals('footer', $structure->handle());
// TODO: Some more assertions
}
#[Test]
public function it_gets_navs()
{
$this->assertEquals(2, Nav::all()->count());
}
#[Test]
public function it_gets_a_nav()
{
$structure = Nav::find('footer');
$this->assertEquals('footer', $structure->handle());
$this->assertEquals([
['entry' => 'pages-home'],
['entry' => 'pages-about'],
['entry' => 'pages-contact'],
], $structure->in('en')->tree());
}
#[Test]
public function it_gets_a_collection_structure()
{
$structure = Structure::find('collection::pages');
$this->assertEquals('pages', $structure->handle());
$this->assertEquals([
['entry' => 'pages-home'],
['entry' => 'pages-about', 'children' => [
['entry' => 'pages-board', 'children' => [
['entry' => 'pages-directors'],
]],
]],
['entry' => 'pages-blog'],
['entry' => 'pages-contact'], // not in the actual tree but since the entry exists it gets added.
], $structure->in('en')->tree());
}
#[Test]
public function it_saves_structures()
{
$structure = Structure::find('footer');
NavRepository::shouldReceive('find')->with('footer');
NavRepository::shouldReceive('save')->with($structure)->once();
$structure->save();
}
#[Test]
public function saving_a_collection_writes_it_to_file()
{
Collection::make('new')
->title('New Collection')
->defaultPublishState(false)
->dated(true)
->revisionsEnabled(true)
->cascade(['foo' => 'bar'])
->save();
$this->assertStringEqualsFile(
$path = __DIR__.'/__fixtures__/content/collections/new.yaml',
"title: 'New Collection'\nrevisions: true\ndate: true\ndefault_status: draft\ninject:\n foo: bar\n"
);
@unlink($path);
}
#[Test]
public function saving_an_asset_container_writes_it_to_file()
{
AssetContainer::make('new')->title('New Container')->save();
$this->assertStringEqualsFile(
$path = __DIR__.'/__fixtures__/content/assets/new.yaml',
"title: 'New Container'\n"
);
@unlink($path);
}
#[Test]
public function saving_a_taxonomy_writes_it_to_file()
{
Taxonomy::make('new')->title('New Taxonomy')->save();
$this->assertStringEqualsFile(
$path = __DIR__.'/__fixtures__/content/taxonomies/new.yaml',
"title: 'New Taxonomy'\n"
);
@unlink($path);
}
#[Test]
public function saving_a_global_set_writes_it_to_file()
{
GlobalSet::make('new')->title('New Global Set')->save();
$this->assertStringEqualsFile(
$path = __DIR__.'/__fixtures__/content/globals/new.yaml',
"title: 'New Global Set'\n"
);
@unlink($path);
@unlink(__DIR__.'/__fixtures__/content/globals/en/new.yaml');
}
#[Test]
public function saving_an_entry_writes_it_to_file()
{
$entry = tap(Entry::make()
->locale('en')
->id('123')
->collection(Collection::findByHandle('blog'))
->slug('test-entry')
->date('2017-07-04')
->data(['title' => 'Test Entry', 'foo' => 'bar'])
)->save();
$this->assertFileExists(__DIR__.'/__fixtures__/content/collections/blog/2017-07-04.test-entry.md');
$entry->delete();
}
#[Test]
public function it_does_not_restore_a_deleted_entry_when_its_cached_item_is_missing()
{
config(['statamic.stache.watcher' => false]);
$entry = tap(Entry::make()
->locale('en')
->id('stale-entry')
->collection(Collection::findByHandle('blog'))
->slug('stale-entry')
->date('2017-07-04')
->data(['title' => 'Stale Entry'])
)->save();
$path = $entry->path();
Entry::all();
File::delete($path);
$this->stache->cacheStore()->forget('stache::items::entries::blog::'.$entry->id());
$entries = Entry::all();
$this->assertCount(14, $entries);
$this->assertFileDoesNotExist($path);
}
#[Test]
public function saving_an_entry_with_a_closure_based_slug_resolves_it_before_writing_to_file()
{
$entry = tap(Entry::make()
->locale('en')
->id('123')
->collection(Collection::findByHandle('blog'))
->slug(function () {
return 'test-entry';
})
->date('2017-07-04')
->data(['title' => 'Test Entry', 'foo' => 'bar'])
)->save();
$this->assertFileExists(__DIR__.'/__fixtures__/content/collections/blog/2017-07-04.test-entry.md');
$entry->delete();
}
}