Skip to content

Commit

Permalink
Merge branch 'feature/RO-277' into 'release/5.2'
Browse files Browse the repository at this point in the history
fixed media search

See merge request heseya/core!381
  • Loading branch information
bvlinsky committed Sep 20, 2023
2 parents f59a5d9 + c8bce40 commit 6368228
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 65 deletions.
11 changes: 7 additions & 4 deletions app/Criteria/MediaSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ class MediaSearch extends Criterion
public function query(Builder $query): Builder
{
if ($this->value) {
$query->where('url', 'like', "%{$this->value}%")
->orWhere('slug', 'like', "%{$this->value}%")
->orWhere('alt', 'like', "%{$this->value}%")
->orWhere('type', 'like', "%{$this->value}%");
$query->where(function (Builder $query): void {
$query
->where('url', 'like', "%{$this->value}%")
->orWhere('slug', 'like', "%{$this->value}%")
->orWhere('alt', 'like', "%{$this->value}%")
->orWhere('type', 'like', "%{$this->value}%");
});
}

return $query;
Expand Down
157 changes: 96 additions & 61 deletions tests/Feature/Media/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,46 @@ class MediaTest extends TestCase
{
private Media $media;

public static function deleteProvider(): array
{
return [
'as user pages add permission' => ['user', 'pages.add'],
'as app pages add permission' => ['application', 'pages.add'],
'as user pages edit permission' => ['user', 'pages.edit'],
'as app pages edit permission' => ['application', 'pages.edit'],
'as user products add permission' => ['user', 'products.add'],
'as app products add permission' => ['application', 'products.add'],
];
}

public static function videoProvider(): array
{
return [
'as user mp4' => ['user', '.mp4', 'video/mp4'],
'as user webm' => ['user', '.webm', 'video/webm'],
'as user ogv' => ['user', '.ogv', 'video/ogg'],
'as user ogg' => ['user', '.ogg', 'video/ogg'],
'as user mov' => ['user', '.mov', 'video/quicktime'],
'as user wmv' => ['user', '.wmv', 'video/x-ms-wmv'],
'as app mp4' => ['application', '.mp4', 'video/mp4'],
'as app webm' => ['application', '.webm', 'video/webm'],
'as app ogv' => ['application', '.ogv', 'video/ogg'],
'as app ogg' => ['application', '.ogg', 'video/ogg'],
'as app mov' => ['application', '.mov', 'video/quicktime'],
'as app wmv' => ['application', '.wmv', 'video/x-ms-wmv'],
];
}

public static function invalidVideoProvider(): array
{
return [
'as user avi' => ['user', '.avi', 'video/x-msvideo'],
'as user ogg audio' => ['user', '.ogg', 'audio/ogg'],
'as app avi' => ['application', '.avi', 'video/x-msvideo'],
'as app ogg audio' => ['application', '.ogg', 'audio/ogg'],
];
}

public function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -70,6 +110,38 @@ public function testIndex($user): void
]);
}

/**
* @dataProvider authProvider
*/
public function testIndexFewFilters($user): void
{
$this->{$user}->givePermissionTo('media.show');

Media::query()->delete();

Media::factory()->create([
'type' => MediaType::OTHER,
'alt' => 'Instrukcja',
]);

$expected = Media::factory()->create([
'type' => MediaType::VIDEO,
'alt' => 'Instrukcja',
]);

Media::factory()->create([
'type' => MediaType::PHOTO,
'alt' => 'test',
]);

$response = $this->actingAs($this->{$user})->json('GET', '/media?search=Instrukcja&type=video');

$response->assertJsonCount(1, 'data')
->assertJsonFragment([
'id' => $expected->getKey(),
]);
}

/**
* @dataProvider authProvider
*/
Expand Down Expand Up @@ -267,14 +339,15 @@ public function testUpload($user): void
'test' => 'value',
],
])
->assertJsonStructure(['data' => [
'id',
'type',
'url',
'slug',
'alt',
'metadata',
],
->assertJsonStructure([
'data' => [
'id',
'type',
'url',
'slug',
'alt',
'metadata',
],
]);
}

Expand Down Expand Up @@ -349,14 +422,15 @@ public function testUploadPdf($user): void
$response
->assertCreated()
->assertJsonFragment(['type' => MediaType::DOCUMENT])
->assertJsonStructure(['data' => [
'id',
'type',
'url',
'slug',
'alt',
'metadata',
],
->assertJsonStructure([
'data' => [
'id',
'type',
'url',
'slug',
'alt',
'metadata',
],
]);
}

Expand Down Expand Up @@ -412,18 +486,6 @@ public function testUploadLegacyPermissionProductsEdit($user): void
])->assertForbidden();
}

public static function deleteProvider(): array
{
return [
'as user pages add permission' => ['user', 'pages.add'],
'as app pages add permission' => ['application', 'pages.add'],
'as user pages edit permission' => ['user', 'pages.edit'],
'as app pages edit permission' => ['application', 'pages.edit'],
'as user products add permission' => ['user', 'products.add'],
'as app products add permission' => ['application', 'products.add'],
];
}

public function testDeleteUnauthorized(): void
{
$this
Expand Down Expand Up @@ -507,24 +569,6 @@ public function testDeleteFromProductSilverboxMedia($user): void
$this->assertDatabaseMissing('media', ['id' => $media->getKey()]);
}

public static function videoProvider(): array
{
return [
'as user mp4' => ['user', '.mp4', 'video/mp4'],
'as user webm' => ['user', '.webm', 'video/webm'],
'as user ogv' => ['user', '.ogv', 'video/ogg'],
'as user ogg' => ['user', '.ogg', 'video/ogg'],
'as user mov' => ['user', '.mov', 'video/quicktime'],
'as user wmv' => ['user', '.wmv', 'video/x-ms-wmv'],
'as app mp4' => ['application', '.mp4', 'video/mp4'],
'as app webm' => ['application', '.webm', 'video/webm'],
'as app ogv' => ['application', '.ogv', 'video/ogg'],
'as app ogg' => ['application', '.ogg', 'video/ogg'],
'as app mov' => ['application', '.mov', 'video/quicktime'],
'as app wmv' => ['application', '.wmv', 'video/x-ms-wmv'],
];
}

/**
* @dataProvider videoProvider
*/
Expand All @@ -544,24 +588,15 @@ public function testUploadVideo($user, $extension, $mime): void
$response
->assertCreated()
->assertJsonFragment(['type' => MediaType::VIDEO])
->assertJsonStructure(['data' => [
'id',
'type',
'url',
],
->assertJsonStructure([
'data' => [
'id',
'type',
'url',
],
]);
}

public static function invalidVideoProvider(): array
{
return [
'as user avi' => ['user', '.avi', 'video/x-msvideo'],
'as user ogg audio' => ['user', '.ogg', 'audio/ogg'],
'as app avi' => ['application', '.avi', 'video/x-msvideo'],
'as app ogg audio' => ['application', '.ogg', 'audio/ogg'],
];
}

/**
* @dataProvider invalidVideoProvider
*/
Expand Down

0 comments on commit 6368228

Please sign in to comment.