Files
pricebuddy/tests/Feature/Widgets/ProductStatsTest.php
Jeremy 8ae1af1bcf fix: address code review findings
- Scope create-form tag IDs to the current user before syncing (ownership guard)
- Validate URL change before persisting price_factor in the urls edit action
- Promote next-check label to days when rounded hours reach 24 (no more "24h")
- Add aria-label/expanded/controls to the category collapse toggle
- Group dashboard products by canonical tag signature to avoid duplicate groups
- Wrap product reorder weight updates in a transaction
2026-07-02 16:53:34 +10:00

392 lines
15 KiB
PHP
Executable File

<?php
namespace Tests\Feature\Widgets;
use App\Filament\Widgets\ProductStats;
use App\Models\Product;
use App\Models\Tag;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ProductStatsTest extends TestCase
{
use RefreshDatabase;
private User $user;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
$this->actingAs($this->user);
}
public function test_products_are_grouped_and_sorted_by_tag_weight(): void
{
// Create tags with different weights
$highWeightTag = Tag::factory()->create([
'name' => 'High Priority',
'weight' => 100,
'user_id' => $this->user->id,
]);
$mediumWeightTag = Tag::factory()->create([
'name' => 'Medium Priority',
'weight' => 50,
'user_id' => $this->user->id,
]);
$lowWeightTag = Tag::factory()->create([
'name' => 'Low Priority',
'weight' => 10,
'user_id' => $this->user->id,
]);
// Create products and attach them to tags
$highPriorityProduct = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'p',
'price_cache' => [['price' => 100.00, 'date' => now()->toDateString()]],
]);
$highPriorityProduct->tags()->attach($highWeightTag);
$mediumPriorityProduct = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'p',
'price_cache' => [['price' => 200.00, 'date' => now()->toDateString()]],
]);
$mediumPriorityProduct->tags()->attach($mediumWeightTag);
$lowPriorityProduct = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'p',
'price_cache' => [['price' => 50.00, 'date' => now()->toDateString()]],
]);
$lowPriorityProduct->tags()->attach($lowWeightTag);
// Get grouped products from the widget
$groups = ProductStats::getProductsGrouped();
// Assert that we have 3 groups
$this->assertCount(3, $groups);
// Assert that groups are sorted by weight (lowest first)
$groupKeys = array_keys($groups);
$this->assertEquals('Low Priority', $groups[$groupKeys[0]]['heading']);
$this->assertEquals('Medium Priority', $groups[$groupKeys[1]]['heading']);
$this->assertEquals('High Priority', $groups[$groupKeys[2]]['heading']);
// Assert weight values are correctly assigned
$this->assertEquals(10, $groups[$groupKeys[0]]['weight']);
$this->assertEquals(50, $groups[$groupKeys[1]]['weight']);
$this->assertEquals(100, $groups[$groupKeys[2]]['weight']);
}
public function test_uncategorized_products_have_zero_weight(): void
{
// Create a tag with weight
$weightedTag = Tag::factory()->create([
'name' => 'Weighted Tag',
'weight' => 75,
'user_id' => $this->user->id,
]);
// Create a product with the weighted tag
$taggedProduct = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'p',
'price_cache' => [['price' => 100.00, 'date' => now()->toDateString()]],
]);
$taggedProduct->tags()->attach($weightedTag);
// Create an uncategorized product (no tags)
$uncategorizedProduct = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'p',
'price_cache' => [['price' => 50.00, 'date' => now()->toDateString()]],
]);
$groups = ProductStats::getProductsGrouped();
$this->assertCount(2, $groups);
// Find the uncategorized group
$uncategorizedGroup = null;
$weightedGroup = null;
foreach ($groups as $group) {
if ($group['heading'] === 'Uncategorized') {
$uncategorizedGroup = $group;
} else {
$weightedGroup = $group;
}
}
$this->assertNotNull($uncategorizedGroup);
$this->assertNotNull($weightedGroup);
$this->assertEquals(0, $uncategorizedGroup['weight']);
$this->assertEquals(75, $weightedGroup['weight']);
// Uncategorized should appear first (weight 0 is lowest)
$groupKeys = array_keys($groups);
$this->assertEquals('Uncategorized', $groups[$groupKeys[0]]['heading']);
$this->assertEquals('Weighted Tag', $groups[$groupKeys[1]]['heading']);
}
public function test_products_with_multiple_tags_use_max_weight(): void
{
// Create tags with different weights
$highWeightTag = Tag::factory()->create([
'name' => 'High Weight',
'weight' => 100,
'user_id' => $this->user->id,
]);
$lowWeightTag = Tag::factory()->create([
'name' => 'Low Weight',
'weight' => 10,
'user_id' => $this->user->id,
]);
// Create a product with both tags
$product = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'p',
'price_cache' => [['price' => 100.00, 'date' => now()->toDateString()]],
]);
$product->tags()->attach([$highWeightTag->id, $lowWeightTag->id]);
$groups = ProductStats::getProductsGrouped();
$this->assertCount(1, $groups);
$group = array_values($groups)[0];
// The group heading should include both tag names
$this->assertStringContainsString('High Weight', $group['heading']);
$this->assertStringContainsString('Low Weight', $group['heading']);
// The weight should be the maximum of the attached tags
$this->assertEquals(100, $group['weight']);
}
public function test_only_favourite_and_published_products_are_included(): void
{
$tag = Tag::factory()->create([
'name' => 'Test Tag',
'weight' => 50,
'user_id' => $this->user->id,
]);
// Create a favourite, published product (should be included)
$includedProduct = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'p',
'price_cache' => [['price' => 100.00, 'date' => now()->toDateString()]],
]);
$includedProduct->tags()->attach($tag);
// Create a non-favourite product (should be excluded)
$excludedProduct1 = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => false,
'status' => 'p',
'price_cache' => [['price' => 100.00, 'date' => now()->toDateString()]],
]);
$excludedProduct1->tags()->attach($tag);
// Create an archived product (should be excluded)
$excludedProduct2 = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'a',
'price_cache' => [['price' => 100.00, 'date' => now()->toDateString()]],
]);
$excludedProduct2->tags()->attach($tag);
$groups = ProductStats::getProductsGrouped();
$this->assertCount(1, $groups);
$group = array_values($groups)[0];
$this->assertCount(1, $group['products']);
$this->assertEquals($includedProduct->id, $group['products']->first()->id);
}
public function test_products_without_price_cache_are_excluded(): void
{
$tag = Tag::factory()->create([
'name' => 'Test Tag',
'weight' => 50,
'user_id' => $this->user->id,
]);
// Create a product without price cache (should be excluded)
$productWithoutPrice = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'p',
'price_cache' => null,
]);
$productWithoutPrice->tags()->attach($tag);
// Create a product with empty price cache (should be excluded)
$productWithEmptyCache = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'p',
'price_cache' => [],
]);
$productWithEmptyCache->tags()->attach($tag);
// Create a product with valid price cache (should be included)
$productWithPrice = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'p',
'price_cache' => [['price' => 100.00, 'date' => now()->toDateString()]],
]);
$productWithPrice->tags()->attach($tag);
$groups = ProductStats::getProductsGrouped();
$this->assertCount(1, $groups);
$group = array_values($groups)[0];
$this->assertCount(1, $group['products']);
$this->assertEquals($productWithPrice->id, $group['products']->first()->id);
}
public function test_tags_with_same_weight_are_ordered_consistently(): void
{
// Create tags with the same weight
$tagA = Tag::factory()->create([
'name' => 'Tag A',
'weight' => 50,
'user_id' => $this->user->id,
]);
$tagB = Tag::factory()->create([
'name' => 'Tag B',
'weight' => 50,
'user_id' => $this->user->id,
]);
// Create products for each tag
$productA = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'p',
'price_cache' => [['price' => 100.00, 'date' => now()->toDateString()]],
'created_at' => now()->subHour(), // Earlier created
]);
$productA->tags()->attach($tagA);
$productB = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'p',
'price_cache' => [['price' => 200.00, 'date' => now()->toDateString()]],
'created_at' => now(), // Later created
]);
$productB->tags()->attach($tagB);
$groups = ProductStats::getProductsGrouped();
$this->assertCount(2, $groups);
// Both should have the same weight
$groupValues = array_values($groups);
$this->assertEquals(50, $groupValues[0]['weight']);
$this->assertEquals(50, $groupValues[1]['weight']);
}
public function test_groups_include_stable_signature(): void
{
$tagA = Tag::factory()->create(['name' => 'Alpha', 'weight' => 10, 'user_id' => $this->user->id]);
$tagB = Tag::factory()->create(['name' => 'Beta', 'weight' => 20, 'user_id' => $this->user->id]);
$product = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'p',
'price_cache' => [['price' => 100.00, 'date' => now()->toDateString()]],
]);
$product->tags()->attach([$tagB->id, $tagA->id]);
$uncategorized = Product::factory()->create([
'user_id' => $this->user->id,
'favourite' => true,
'status' => 'p',
'price_cache' => [['price' => 50.00, 'date' => now()->toDateString()]],
]);
$groups = ProductStats::getProductsGrouped();
$signatures = array_column($groups, 'signature');
// Sorted tag ids joined by '-', regardless of attach order.
$this->assertContains(min($tagA->id, $tagB->id).'-'.max($tagA->id, $tagB->id), $signatures);
$this->assertContains('uncategorized', $signatures);
}
public function test_view_data_orders_categories_by_user_preference(): void
{
$tagA = Tag::factory()->create(['name' => 'Alpha', 'weight' => 10, 'user_id' => $this->user->id]);
$tagB = Tag::factory()->create(['name' => 'Beta', 'weight' => 20, 'user_id' => $this->user->id]);
$pa = Product::factory()->create(['user_id' => $this->user->id, 'favourite' => true, 'status' => 'p', 'price_cache' => [['price' => 1, 'date' => now()->toDateString(), 'history' => []]]]);
$pa->tags()->attach($tagA);
$pb = Product::factory()->create(['user_id' => $this->user->id, 'favourite' => true, 'status' => 'p', 'price_cache' => [['price' => 1, 'date' => now()->toDateString(), 'history' => []]]]);
$pb->tags()->attach($tagB);
// User prefers Beta before Alpha (opposite of tag weight).
(new \App\Services\Dashboard\DashboardLayoutService($this->user))
->setCategoryOrder([(string) $tagB->id, (string) $tagA->id]);
$data = \Livewire\Livewire::test(ProductStats::class)->instance()->getViewData();
$headings = array_column($data['groups'], 'heading');
$this->assertSame(['Beta', 'Alpha'], $headings);
}
public function test_view_data_marks_collapsed_categories(): void
{
$tag = Tag::factory()->create(['name' => 'Alpha', 'weight' => 10, 'user_id' => $this->user->id]);
$p = Product::factory()->create(['user_id' => $this->user->id, 'favourite' => true, 'status' => 'p', 'price_cache' => [['price' => 1, 'date' => now()->toDateString(), 'history' => []]]]);
$p->tags()->attach($tag);
(new \App\Services\Dashboard\DashboardLayoutService($this->user))->toggleCategoryCollapse((string) $tag->id);
$data = \Livewire\Livewire::test(ProductStats::class)->instance()->getViewData();
$this->assertTrue($data['groups'][0]['collapsed']);
}
public function test_same_tag_set_produces_one_group_regardless_of_attach_order(): void
{
$alpha = Tag::factory()->create(['name' => 'Alpha', 'weight' => 10, 'user_id' => $this->user->id]);
$beta = Tag::factory()->create(['name' => 'Beta', 'weight' => 10, 'user_id' => $this->user->id]);
$first = Product::factory()->create(['user_id' => $this->user->id, 'favourite' => true, 'status' => 'p', 'price_cache' => [['price' => 1, 'date' => now()->toDateString()]]]);
$first->tags()->attach([$alpha->id, $beta->id]);
// Same tag set, attached in the opposite order.
$second = Product::factory()->create(['user_id' => $this->user->id, 'favourite' => true, 'status' => 'p', 'price_cache' => [['price' => 2, 'date' => now()->toDateString()]]]);
$second->tags()->attach([$beta->id, $alpha->id]);
$groups = ProductStats::getProductsGrouped();
// One canonical group containing both products, with a stable heading.
$this->assertCount(1, $groups);
$group = array_values($groups)[0];
$this->assertSame('Alpha, Beta', $group['heading']);
$this->assertCount(2, $group['products']);
}
}