Skip to content

Commit 4fa41d8

Browse files
authored
Merge pull request #1469 from KodeStar/2.x
Add tests and fix user edit form
2 parents 53fd624 + 1e6b1f6 commit 4fa41d8

File tree

6 files changed

+137
-5
lines changed

6 files changed

+137
-5
lines changed

app/Http/Controllers/SearchController.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,23 @@ public function index(Request $request)
1818
$requestprovider = $request->input('provider');
1919
$query = $request->input('q');
2020

21+
// Validate the presence and non-emptiness of the query parameter
22+
if (!$query || trim($query) === '') {
23+
abort(400, 'Missing or empty query parameter');
24+
}
25+
2126
$provider = Search::providerDetails($requestprovider);
2227

28+
if (!$provider || !isset($provider->type)) {
29+
abort(404, 'Invalid provider');
30+
}
31+
2332
if ($provider->type == 'standard') {
2433
return redirect($provider->url.'?'.$provider->query.'='.urlencode($query));
2534
} elseif ($provider->type == 'external') {
2635
$class = new $provider->class;
27-
//print_r($provider);
2836
return $class->getResults($query, $provider);
2937
}
3038

31-
//print_r($provider);
32-
}
39+
abort(404, 'Provider type not supported');}
3340
}

database/seeders/UsersSeeder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function run(): void
1818
$user->username = 'admin';
1919
$user->email = '[email protected]';
2020
$user->password = null;
21+
$user->public_front = 0; // Default value for public_front
2122
$user->save();
2223

2324
$user_id = $user->id;

resources/views/users/form.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@
4545
<div style="margin-top: -40px; width: 100%; padding: 0" class="create">
4646
<div class="input">
4747
<label>{{ __('app.apps.password') }} *</label>
48-
{{ html()->password('password', array('class' => 'form-control'))->attributes(null) }}
48+
{{ html()->password('password', array('class' => 'form-control'))->attributes([]) }}
4949
<hr />
5050

5151
</div>
5252
<div class="input">
5353
<label>{{ __('app.user.password_confirm') }} *</label>
54-
{{ html()->password('password_confirmation', array('class' => 'form-control'))->attributes(null) }}
54+
{{ html()->password('password_confirmation', array('class' => 'form-control'))->attributes([]) }}
5555
</div>
5656
</div>
5757

tests/Feature/ImportTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Tests\TestCase;
7+
8+
class ImportTest extends TestCase
9+
{
10+
use RefreshDatabase;
11+
12+
public function test_import_page_loads_successfully(): void
13+
{
14+
$response = $this->get(route('items.import'));
15+
16+
$response->assertStatus(200);
17+
$response->assertSee('Import');
18+
}
19+
}

tests/Feature/SearchTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Tests\TestCase;
7+
8+
class SearchTest extends TestCase
9+
{
10+
use RefreshDatabase;
11+
12+
public function test_search_page_redirects_correctly(): void
13+
{
14+
$provider = 'google'; // Example provider
15+
$query = 'test'; // Example search term
16+
17+
$response = $this->get(route('search', ['provider' => $provider, 'q' => $query]));
18+
19+
$response->assertStatus(302);
20+
21+
$expectedUrl = 'https://www.google.com/search?q=' . urlencode($query);
22+
$response->assertRedirect($expectedUrl);
23+
}
24+
25+
public function test_search_page_with_invalid_provider(): void
26+
{
27+
$provider = 'invalid_provider'; // Invalid provider
28+
$query = 'test'; // Example search term
29+
30+
$response = $this->get(route('search', ['provider' => $provider, 'q' => $query]));
31+
32+
$response->assertStatus(404); // Assert that the response status is 404
33+
}
34+
35+
public function test_search_page_without_query_parameter(): void
36+
{
37+
$provider = 'google'; // Example provider
38+
39+
$response = $this->get(route('search', ['provider' => $provider]));
40+
41+
$response->assertStatus(400); // Assert that the response status is 400 (Bad Request)
42+
}
43+
44+
public function test_search_page_with_empty_query(): void
45+
{
46+
$provider = 'google'; // Example provider
47+
$query = ''; // Empty search term
48+
49+
$response = $this->get(route('search', ['provider' => $provider, 'q' => $query]));
50+
51+
$response->assertStatus(400); // Assert that the response status is 400 (Bad Request)
52+
}
53+
}

tests/Feature/UserEditTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use App\User;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Tests\TestCase;
8+
9+
class UserEditTest extends TestCase
10+
{
11+
use RefreshDatabase;
12+
13+
public function test_user_edit_page_loads_successfully(): void
14+
{
15+
$this->seed();
16+
17+
$user = User::factory()->create();
18+
19+
$response = $this->get(route('users.edit', $user->id));
20+
21+
$response->assertStatus(200);
22+
$response->assertSee($user->username); // Verify username is displayed
23+
$response->assertSee(__('app.user.email')); // Verify email field is present
24+
}
25+
26+
public function test_user_can_be_updated(): void
27+
{
28+
$this->seed();
29+
30+
$user = User::factory()->create([
31+
'username' => 'oldusername',
32+
'email' => '[email protected]',
33+
]);
34+
35+
$data = [
36+
'username' => 'newusername',
37+
'email' => '[email protected]',
38+
'password' => 'newpassword',
39+
'password_confirmation' => 'newpassword',
40+
'public_front' => 1, // Include public_front in the test data
41+
];
42+
43+
$response = $this->patch(route('users.update', $user->id), $data);
44+
45+
$response->assertRedirect(route('dash'));
46+
$this->assertDatabaseHas('users', [
47+
'id' => $user->id,
48+
'username' => 'newusername',
49+
'email' => '[email protected]',
50+
]);
51+
}
52+
}

0 commit comments

Comments
 (0)