|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use App\Events\TagFrequencyChanged; |
| 6 | +use App\Models\ComputerScienceResource; |
| 7 | +use App\Models\ResourceEdits; |
| 8 | +use App\Models\TagFrequency; |
| 9 | +use App\Models\User; |
| 10 | +use App\Services\ResourceEditsService; |
| 11 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 12 | +use Illuminate\Foundation\Testing\WithFaker; |
| 13 | +use Mockery; |
| 14 | +use Tests\TestCase; |
| 15 | +use Tests\TestResources\ComputerScienceResourceTestResource; |
| 16 | + |
| 17 | +class TagSearchTest extends TestCase |
| 18 | +{ |
| 19 | + use RefreshDatabase; |
| 20 | + |
| 21 | + protected $user; |
| 22 | + |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + parent::setUp(); |
| 26 | + $this->user = User::factory()->create(); |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + public function test_can_search_tags_by_prefix() |
| 31 | + { |
| 32 | + TagFrequencyChanged::dispatch(null, ['python' => 100, 'pygame' => 50, 'java' => 500]); |
| 33 | + |
| 34 | + $response = $this->getJson(route('tags.search', ['query' => 'py'])); |
| 35 | + |
| 36 | + $response->assertStatus(200); |
| 37 | + $response->assertJsonCount(2, 'tags'); // only 'python' and 'pygame' |
| 38 | + |
| 39 | + $response->assertJsonFragment(['tag' => 'python', 'count' => 100]); |
| 40 | + $response->assertJsonFragment(['tag' => 'pygame', 'count' => 50]); |
| 41 | + |
| 42 | + // Ensure order by count descending |
| 43 | + $tags = collect($response->json('tags')); |
| 44 | + $this->assertEquals(['python', 'pygame'], $tags->pluck('tag')->toArray()); |
| 45 | + } |
| 46 | + |
| 47 | + public function test_query_too_long_returns_422() |
| 48 | + { |
| 49 | + $query = str_repeat('a', 51); // too long |
| 50 | + |
| 51 | + $response = $this->getJson(route('tags.search', ['query' => $query])); |
| 52 | + $response->assertStatus(422); |
| 53 | + } |
| 54 | + |
| 55 | + public function test_creating_resource_updates_tag_frequency() |
| 56 | + { |
| 57 | + $this->actingAs($this->user); |
| 58 | + |
| 59 | + $formData = ComputerScienceResourceTestResource::fake([ |
| 60 | + 'topic_tags' => ['python', 'algorithms', 'java'], |
| 61 | + 'programming_language_tags' => ['python'], |
| 62 | + 'general_tags' => ['beginner'] |
| 63 | + ]); |
| 64 | + |
| 65 | + $response = $this->postJson(route('resources.store'), $formData); |
| 66 | + |
| 67 | + $response->assertStatus(302); // a redirect after successful creation |
| 68 | + $response->assertRedirect(); |
| 69 | + |
| 70 | + // Check that TagFrequency reflects counts |
| 71 | + $this->assertDatabaseHas('tag_frequencies', ['tag' => 'python', 'count' => 2]); |
| 72 | + $this->assertDatabaseHas('tag_frequencies', ['tag' => 'algorithms', 'count' => 1]); |
| 73 | + $this->assertDatabaseHas('tag_frequencies', ['tag' => 'beginner', 'count' => 1]); |
| 74 | + } |
| 75 | + |
| 76 | + public function test_dispatching_tag_frequency_change_removes_unused_tags() |
| 77 | + { |
| 78 | + // Step 1: Add initial tags via dispatch |
| 79 | + TagFrequencyChanged::dispatch(null, [ |
| 80 | + 'python' => 2, |
| 81 | + 'java' => 1, |
| 82 | + 'ruby' => 1, |
| 83 | + ]); |
| 84 | + |
| 85 | + $this->assertDatabaseHas('tag_frequencies', ['tag' => 'python', 'count' => 2]); |
| 86 | + $this->assertDatabaseHas('tag_frequencies', ['tag' => 'java', 'count' => 1]); |
| 87 | + $this->assertDatabaseHas('tag_frequencies', ['tag' => 'ruby', 'count' => 1]); |
| 88 | + |
| 89 | + // Step 2: Dispatch with zero counts to simulate removal |
| 90 | + TagFrequencyChanged::dispatch([ |
| 91 | + 'python' => 2, |
| 92 | + 'java' => 1, |
| 93 | + 'ruby' => 1, |
| 94 | + ], []); // no tags used now |
| 95 | + |
| 96 | + // Step 3: Ensure all tag frequencies are removed |
| 97 | + $this->assertDatabaseMissing('tag_frequencies', ['tag' => 'python']); |
| 98 | + $this->assertDatabaseMissing('tag_frequencies', ['tag' => 'java']); |
| 99 | + $this->assertDatabaseMissing('tag_frequencies', ['tag' => 'ruby']); |
| 100 | + |
| 101 | + // Optional: search should return empty |
| 102 | + $response = $this->getJson(route('tags.search', ['query' => 'py'])); |
| 103 | + $response->assertStatus(200); |
| 104 | + $this->assertEmpty($response->json('tags')); |
| 105 | + } |
| 106 | + |
| 107 | + public function test_merging_edit_correctly_updates_tag_frequency() |
| 108 | + { |
| 109 | + $resource = ComputerScienceResource::factory() |
| 110 | + ->setTags( |
| 111 | + ['algorithms', 'tag1', 'tag2'], |
| 112 | + ['c++'], |
| 113 | + ['reference'] |
| 114 | + ) |
| 115 | + ->create(); |
| 116 | + |
| 117 | + $this->actingAs($this->user); |
| 118 | + |
| 119 | + // Create an edit with new tags |
| 120 | + $editData = ComputerScienceResourceTestResource::fake([ |
| 121 | + 'topic_tags' => ['python', 'algorithms', 'tag1'], // 'algorithms' already exists, 'python' is new |
| 122 | + 'programming_language_tags' => ['python'], // replacing 'c++' |
| 123 | + 'general_tags' => ['tutorial'], |
| 124 | + ]); |
| 125 | + $editData['edit_title'] = 'Tag Update'; |
| 126 | + $editData['edit_description'] = 'Tag change for test'; |
| 127 | + |
| 128 | + // Create the edit |
| 129 | + $response = $this->post(route('resource_edits.store', ['computerScienceResource' => $resource->id]), $editData); |
| 130 | + $response->assertStatus(302); |
| 131 | + |
| 132 | + $edit = ResourceEdits::latest()->first(); |
| 133 | + |
| 134 | + // Mock approval |
| 135 | + $this->instance(ResourceEditsService::class, Mockery::mock(ResourceEditsService::class, function ($mock) { |
| 136 | + $mock->shouldReceive('canMergeEdits')->andReturnTrue(); |
| 137 | + })); |
| 138 | + |
| 139 | + // Merge the edit |
| 140 | + $mergeResponse = $this->post(route('resource_edits.merge', ['resourceEdits' => $edit->id])); |
| 141 | + $mergeResponse->assertStatus(302); |
| 142 | + |
| 143 | + // TagFrequency should now reflect the changes |
| 144 | + $this->assertEquals(2, TagFrequency::where('tag', 'python')->value('count')); |
| 145 | + $this->assertEquals(1, TagFrequency::where('tag', 'algorithms')->value('count')); // Still used once |
| 146 | + $this->assertDatabaseMissing('tag_frequencies', ['tag' => 'c++']); // Removed |
| 147 | + $this->assertEquals(1, TagFrequency::where('tag', 'tutorial')->value('count')); |
| 148 | + } |
| 149 | +} |
0 commit comments