|
3 | 3 | use Eclipse\Catalogue\Models\PriceList; |
4 | 4 | use Eclipse\Catalogue\Models\PriceListData; |
5 | 5 | use Eclipse\World\Models\Currency; |
| 6 | +use Workbench\App\Models\Site; |
6 | 7 |
|
7 | 8 | beforeEach(function () { |
8 | 9 | // Create test currencies |
|
19 | 20 | ]); |
20 | 21 | }); |
21 | 22 |
|
| 23 | +/** |
| 24 | + * Helper: create a PriceListData row including the tenant foreign key when |
| 25 | + * tenancy is enabled. Keeps the tests readable and future-proof. |
| 26 | + */ |
| 27 | +function createPriceListData(array $attributes): PriceListData |
| 28 | +{ |
| 29 | + $tenantFK = config('eclipse-catalogue.tenancy.foreign_key'); |
| 30 | + if ($tenantFK) { |
| 31 | + $siteId = Site::first()?->id; |
| 32 | + // Ensure we always include the tenant FK when required |
| 33 | + $attributes[$tenantFK] = $attributes[$tenantFK] ?? $siteId; |
| 34 | + } |
| 35 | + |
| 36 | + return PriceListData::create($attributes); |
| 37 | +} |
| 38 | + |
22 | 39 | // Access Control Tests |
23 | 40 | test('unauthenticated users cannot access price list index', function () { |
24 | 41 | // Clear any authenticated user |
|
122 | 139 | test('has price list data relationship', function () { |
123 | 140 | $priceList = PriceList::factory()->create(['currency_id' => 'USD', 'tax_included' => false]); |
124 | 141 |
|
125 | | - PriceListData::create([ |
| 142 | + createPriceListData([ |
126 | 143 | 'price_list_id' => $priceList->id, |
127 | 144 | 'is_active' => true, |
128 | 145 | 'is_default' => false, |
|
157 | 174 | $priceList2 = PriceList::factory()->create(['currency_id' => 'USD', 'tax_included' => false]); |
158 | 175 |
|
159 | 176 | // Create price list data for both as default selling |
160 | | - PriceListData::create([ |
| 177 | + createPriceListData([ |
161 | 178 | 'price_list_id' => $priceList1->id, |
162 | 179 | 'is_active' => true, |
163 | 180 | 'is_default' => true, |
164 | 181 | 'is_default_purchase' => false, |
165 | 182 | ]); |
166 | 183 |
|
167 | | - PriceListData::create([ |
| 184 | + createPriceListData([ |
168 | 185 | 'price_list_id' => $priceList2->id, |
169 | 186 | 'is_active' => true, |
170 | 187 | 'is_default' => true, |
|
188 | 205 | $priceList2 = PriceList::factory()->create(['currency_id' => 'USD', 'tax_included' => false]); |
189 | 206 |
|
190 | 207 | // Create price list data for both as default purchase |
191 | | - PriceListData::create([ |
| 208 | + createPriceListData([ |
192 | 209 | 'price_list_id' => $priceList1->id, |
193 | 210 | 'is_active' => true, |
194 | 211 | 'is_default' => false, |
195 | 212 | 'is_default_purchase' => true, |
196 | 213 | ]); |
197 | 214 |
|
198 | | - PriceListData::create([ |
| 215 | + createPriceListData([ |
199 | 216 | 'price_list_id' => $priceList2->id, |
200 | 217 | 'is_active' => true, |
201 | 218 | 'is_default' => false, |
|
216 | 233 | $sellingPriceList = PriceList::factory()->create(['currency_id' => 'USD', 'tax_included' => false]); |
217 | 234 | $purchasePriceList = PriceList::factory()->create(['currency_id' => 'USD', 'tax_included' => false]); |
218 | 235 |
|
219 | | - PriceListData::create([ |
| 236 | + createPriceListData([ |
220 | 237 | 'price_list_id' => $sellingPriceList->id, |
221 | 238 | 'is_active' => true, |
222 | 239 | 'is_default' => true, |
223 | 240 | 'is_default_purchase' => false, |
224 | 241 | ]); |
225 | 242 |
|
226 | | - PriceListData::create([ |
| 243 | + createPriceListData([ |
227 | 244 | 'price_list_id' => $purchasePriceList->id, |
228 | 245 | 'is_active' => true, |
229 | 246 | 'is_default' => false, |
|
243 | 260 | $priceList = PriceList::factory()->create(['currency_id' => 'USD', 'tax_included' => false]); |
244 | 261 |
|
245 | 262 | // Create data that violates the business rule |
246 | | - $priceListData = PriceListData::create([ |
| 263 | + $priceListData = createPriceListData([ |
247 | 264 | 'price_list_id' => $priceList->id, |
248 | 265 | 'is_active' => true, |
249 | 266 | 'is_default' => true, |
|
259 | 276 | test('get default selling price list works', function () { |
260 | 277 | $priceList = PriceList::factory()->create(['currency_id' => 'USD', 'tax_included' => false]); |
261 | 278 |
|
262 | | - PriceListData::create([ |
| 279 | + createPriceListData([ |
263 | 280 | 'price_list_id' => $priceList->id, |
264 | 281 | 'is_active' => true, |
265 | 282 | 'is_default' => true, |
|
275 | 292 | test('get default purchase price list works', function () { |
276 | 293 | $priceList = PriceList::factory()->create(['currency_id' => 'USD', 'tax_included' => false]); |
277 | 294 |
|
278 | | - PriceListData::create([ |
| 295 | + createPriceListData([ |
279 | 296 | 'price_list_id' => $priceList->id, |
280 | 297 | 'is_active' => true, |
281 | 298 | 'is_default' => false, |
|
0 commit comments