|
2 | 2 |
|
3 | 3 | namespace Tests\Entries; |
4 | 4 |
|
| 5 | +use Facades\Statamic\Fields\BlueprintRepository; |
5 | 6 | use Illuminate\Foundation\Testing\RefreshDatabase; |
6 | 7 | use Statamic\Eloquent\Collections\Collection; |
7 | 8 | use Statamic\Eloquent\Entries\Entry; |
8 | 9 | use Statamic\Eloquent\Entries\EntryModel; |
| 10 | +use Statamic\Facades; |
9 | 11 | use Tests\TestCase; |
10 | 12 |
|
11 | 13 | class EntryTest extends TestCase |
@@ -56,4 +58,127 @@ public function it_saves_to_entry_model() |
56 | 58 |
|
57 | 59 | $this->assertEquals(collect($model->toArray())->except(['updated_at'])->all(), collect($entry->toModel()->toArray())->except('updated_at')->all()); |
58 | 60 | } |
| 61 | + |
| 62 | + /** @test */ |
| 63 | + public function it_propagates_entry_if_configured() |
| 64 | + { |
| 65 | + Facades\Site::setConfig([ |
| 66 | + 'default' => 'en', |
| 67 | + 'sites' => [ |
| 68 | + 'en' => ['name' => 'English', 'locale' => 'en_US', 'url' => 'http://test.com/'], |
| 69 | + 'fr' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => 'http://fr.test.com/'], |
| 70 | + 'es' => ['name' => 'Spanish', 'locale' => 'es_ES', 'url' => 'http://test.com/es/'], |
| 71 | + 'de' => ['name' => 'German', 'locale' => 'de_DE', 'url' => 'http://test.com/de/'], |
| 72 | + ], |
| 73 | + ]); |
| 74 | + |
| 75 | + $collection = (new Collection) |
| 76 | + ->handle('pages') |
| 77 | + ->propagate(true) |
| 78 | + ->sites(['en', 'fr', 'de']) |
| 79 | + ->save(); |
| 80 | + |
| 81 | + $entry = (new Entry) |
| 82 | + ->id(1) |
| 83 | + ->locale('en') |
| 84 | + ->collection($collection); |
| 85 | + |
| 86 | + $return = $entry->save(); |
| 87 | + |
| 88 | + $this->assertIsObject($fr = $entry->descendants()->get('fr')); |
| 89 | + $this->assertIsObject($de = $entry->descendants()->get('de')); |
| 90 | + $this->assertNull($entry->descendants()->get('es')); // collection not configured for this site |
| 91 | + } |
| 92 | + |
| 93 | + /** @test */ |
| 94 | + public function it_propagates_updating_origin_data_to_descendent_models() |
| 95 | + { |
| 96 | + Facades\Site::setConfig([ |
| 97 | + 'default' => 'en', |
| 98 | + 'sites' => [ |
| 99 | + 'en' => ['name' => 'English', 'locale' => 'en_US', 'url' => 'http://test.com/'], |
| 100 | + 'fr' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => 'http://fr.test.com/'], |
| 101 | + 'es' => ['name' => 'Spanish', 'locale' => 'es_ES', 'url' => 'http://test.com/es/'], |
| 102 | + 'de' => ['name' => 'German', 'locale' => 'de_DE', 'url' => 'http://test.com/de/'], |
| 103 | + ], |
| 104 | + ]); |
| 105 | + |
| 106 | + $blueprint = Facades\Blueprint::makeFromFields(['foo' => ['type' => 'test', 'localizable' => true]])->setHandle('test'); |
| 107 | + $blueprint->save(); |
| 108 | + |
| 109 | + BlueprintRepository::shouldReceive('in')->with('collections/pages')->andReturn(collect(['test' => $blueprint])); |
| 110 | + |
| 111 | + $collection = (new Collection) |
| 112 | + ->handle('pages') |
| 113 | + ->propagate(true) |
| 114 | + ->sites(['en', 'fr', 'de']) |
| 115 | + ->save(); |
| 116 | + |
| 117 | + $entry = (new Entry) |
| 118 | + ->id(1) |
| 119 | + ->locale('en') |
| 120 | + ->collection($collection) |
| 121 | + ->blueprint('test') |
| 122 | + ->data([ |
| 123 | + 'foo' => 'bar', |
| 124 | + 'roo' => 'rar', |
| 125 | + ]); |
| 126 | + |
| 127 | + $return = $entry->save(); |
| 128 | + |
| 129 | + $this->assertNull($entry->descendants()->get('fr')->model()->data['too'] ?? null); |
| 130 | + $this->assertNull($entry->descendants()->get('de')->model()->data['too'] ?? null); |
| 131 | + |
| 132 | + $blueprint->ensureField('too', ['type' => 'test', 'localizable' => true]); |
| 133 | + $entry->merge(['too' => 'tar']); |
| 134 | + |
| 135 | + Facades\Entry::save($entry); |
| 136 | + |
| 137 | + $this->assertNotNull($entry->descendants()->get('fr')->model()->data['too'] ?? null); |
| 138 | + $this->assertNotNull($entry->descendants()->get('de')->model()->data['too'] ?? null); |
| 139 | + } |
| 140 | + |
| 141 | + /** @test */ |
| 142 | + public function it_propagates_origin_date_to_descendent_models() |
| 143 | + { |
| 144 | + Facades\Site::setConfig([ |
| 145 | + 'default' => 'en', |
| 146 | + 'sites' => [ |
| 147 | + 'en' => ['name' => 'English', 'locale' => 'en_US', 'url' => 'http://test.com/'], |
| 148 | + 'fr' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => 'http://fr.test.com/'], |
| 149 | + 'es' => ['name' => 'Spanish', 'locale' => 'es_ES', 'url' => 'http://test.com/es/'], |
| 150 | + 'de' => ['name' => 'German', 'locale' => 'de_DE', 'url' => 'http://test.com/de/'], |
| 151 | + ], |
| 152 | + ]); |
| 153 | + |
| 154 | + $blueprint = Facades\Blueprint::makeFromFields(['foo' => ['type' => 'test', 'localizable' => true]])->setHandle('test'); |
| 155 | + $blueprint->save(); |
| 156 | + |
| 157 | + BlueprintRepository::shouldReceive('in')->with('collections/pages')->andReturn(collect(['test' => $blueprint])); |
| 158 | + |
| 159 | + $collection = (new Collection) |
| 160 | + ->handle('pages') |
| 161 | + ->dated(true) |
| 162 | + ->propagate(true) |
| 163 | + ->sites(['en', 'fr', 'de']) |
| 164 | + ->save(); |
| 165 | + |
| 166 | + $entry = (new Entry) |
| 167 | + ->id(1) |
| 168 | + ->collection($collection) |
| 169 | + ->date('2023-01-01') |
| 170 | + ->locale('en') |
| 171 | + ->blueprint('test'); |
| 172 | + |
| 173 | + $return = $entry->save(); |
| 174 | + |
| 175 | + $this->assertEquals($entry->descendants()->get('fr')->model()->date, '2023-01-01 00:00:00'); |
| 176 | + |
| 177 | + $blueprint->ensureField('too', ['type' => 'test', 'localizable' => true]); |
| 178 | + $entry->date('2024-01-01'); |
| 179 | + |
| 180 | + Facades\Entry::save($entry); |
| 181 | + |
| 182 | + $this->assertEquals($entry->descendants()->get('fr')->model()->date, '2024-01-01 00:00:00'); |
| 183 | + } |
59 | 184 | } |
0 commit comments