Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some tests #1395

Merged
merged 24 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tests/Feature/LangTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
namespace Tests\Feature;

use App\Facades\Lang;
use App\Factories\LangFactory;
use App\Models\Configs;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class LangTest extends TestCase
Expand All @@ -35,5 +38,24 @@ public function testLang(): void
static::assertArrayHasKey($key, $locale, 'Language ' . $lang_test->code() . ' is incomplete.');
}
}

static::assertEquals('en', Lang::get_code());
static::assertEquals('OK', Lang::get('SUCCESS'));
}

public function testEnglishAsFallbackIfLangConfigIsMissing(): void
{
Configs::where('key', '=', 'lang')->delete();
$lang = new \App\Locale\Lang(new LangFactory());
self::assertEquals('en', $lang->get_code());

DB::table('configs')->insert([
[
'key' => 'lang',
'value' => 'en',
'confidentiality' => 0,
'cat' => 'Gallery',
],
]);
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
}
}
219 changes: 219 additions & 0 deletions tests/Feature/SearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
<?php

/**
* We don't care for unhandled exceptions in tests.
* It is the nature of a test to throw an exception.
* Without this suppression we had 100+ Linter warning in this file which
* don't help anything.
*
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/

namespace Tests\Feature;

use Tests\Feature\Base\PhotoTestBase;
use Tests\Feature\Traits\RequiresEmptyAlbums;
use Tests\TestCase;

class SearchTest extends PhotoTestBase
{
use RequiresEmptyAlbums;

public function setUp(): void
{
parent::setUp();
$this->setUpRequiresEmptyAlbums();
}

public function tearDown(): void
{
$this->tearDownRequiresEmptyPhotos();
$this->tearDownRequiresEmptyAlbums();
parent::tearDown();
}

public function testSearchPhotoByTitle(): void
{
$photoId1 = $this->photos_tests->upload(
TestCase::createUploadedFile(TestCase::SAMPLE_FILE_NIGHT_IMAGE)
)->offsetGet('id');
$photoId2 = $this->photos_tests->upload(
TestCase::createUploadedFile(TestCase::SAMPLE_FILE_MONGOLIA_IMAGE)
)->offsetGet('id');
$this->photos_tests->set_title($photoId1, 'photo search');
$this->photos_tests->set_title($photoId2, 'do not find me');

$response = $this->postJson(
'/api/Search::run',
['term' => 'search']
);
$response->assertStatus(200);

$response->assertJson([
nagmat84 marked this conversation as resolved.
Show resolved Hide resolved
'photos' => [
[
'album_id' => null,
'aperture' => 'f/2.8',
'focal' => '16 mm',
'id' => $photoId1,
'iso' => '1250',
'lens' => 'EF16-35mm f/2.8L USM',
'make' => 'Canon',
'model' => 'Canon EOS R',
'shutter' => '30 s',
'title' => 'photo search',
'type' => 'image/jpeg',
'size_variants' => [
'small' => [
'width' => 540,
'height' => 360,
],
'medium' => [
'width' => 1620,
'height' => 1080,
],
'original' => [
'width' => 6720,
'height' => 4480,
'filesize' => 21106422,
],
],
],
],
]);

$response->assertJsonMissing([
'title' => 'do not find me',
]);

$response->assertJsonMissing([
'id' => $photoId2,
]);
}

public function testSearchPhotoByTag(): void
{
$photoId1 = $this->photos_tests->upload(
TestCase::createUploadedFile(TestCase::SAMPLE_FILE_NIGHT_IMAGE)
)->offsetGet('id');
$photoId2 = $this->photos_tests->upload(
TestCase::createUploadedFile(TestCase::SAMPLE_FILE_MONGOLIA_IMAGE)
)->offsetGet('id');
$this->photos_tests->set_title($photoId1, 'photo search');
$this->photos_tests->set_title($photoId2, 'do not find me');
$this->photos_tests->set_tag([$photoId1], ['search tag']);
$this->photos_tests->set_tag([$photoId2], ['other tag']);

$response = $this->postJson(
'/api/Search::run',
['term' => 'search']
);
$response->assertStatus(200);

$response->assertJson([
'photos' => [
[
'album_id' => null,
'aperture' => 'f/2.8',
'focal' => '16 mm',
'id' => $photoId1,
'iso' => '1250',
'lens' => 'EF16-35mm f/2.8L USM',
'make' => 'Canon',
'model' => 'Canon EOS R',
'shutter' => '30 s',
'tags' => ['search tag'],
'type' => 'image/jpeg',
'size_variants' => [
'small' => [
'width' => 540,
'height' => 360,
],
'medium' => [
'width' => 1620,
'height' => 1080,
],
'original' => [
'width' => 6720,
'height' => 4480,
'filesize' => 21106422,
],
],
],
],
]);

$response->assertJsonMissing([
'title' => 'do not find me',
]);

$response->assertJsonMissing([
'tags' => ['other tag'],
]);

$response->assertJsonMissing([
'id' => $photoId2,
]);
}

public function testSearchAlbumByTitle(): void
{
/** @var string $albumId1 */
$albumId1 = $this->albums_tests->add(null, 'search')->offsetGet('id');
/** @var string $albumId2 */
$albumId2 = $this->albums_tests->add(null, 'other')->offsetGet('id');

$response = $this->postJson(
'/api/Search::run',
['term' => 'search']
);
$response->assertStatus(200);

$response->assertJson([
'albums' => [[
'id' => $albumId1,
'title' => 'search',
]],
]);

$response->assertJsonMissing([
'title' => 'other',
]);

$response->assertJsonMissing([
'id' => $albumId2,
]);
}

public function testSearchTagAlbumByTitle(): void
{
/** @var string $tagAlbumId1 */
$tagAlbumId1 = $this->albums_tests->addByTags('tag search', ['tag1', 'tag2'])->offsetGet('id');
/** @var string $tagAlbumId2 */
$tagAlbumId2 = $this->albums_tests->addByTags('tag other', ['tag3'])->offsetGet('id');

$response = $this->postJson(
'/api/Search::run',
['term' => 'search']
);
$response->assertStatus(200);

$response->assertJson([
'tag_albums' => [
[
'id' => $tagAlbumId1,
'title' => 'tag search',
],
],
]);

$response->assertJsonMissing([
'title' => 'tag other',
]);

$response->assertJsonMissing([
'id' => $tagAlbumId2,
]);
}
}
90 changes: 90 additions & 0 deletions tests/Feature/SettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* We don't care for unhandled exceptions in tests.
* It is the nature of a test to throw an exception.
* Without this suppression we had 100+ Linter warning in this file which
* don't help anything.
*
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/

namespace Tests\Feature;

use App\DTO\SortingCriterion;
use App\Facades\AccessControl;
use App\Http\Requests\Settings\SetSortingRequest;
use Tests\TestCase;

class SettingsTest extends TestCase
{
public function testSetSorting(): void
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
{
AccessControl::log_as_id(0);

$this->postJson('/api/Settings::setSorting',
[
SetSortingRequest::ALBUM_SORTING_COLUMN_ATTRIBUTE => SortingCriterion::COLUMN_CREATED_AT,
SetSortingRequest::PHOTO_SORTING_COLUMN_ATTRIBUTE => SortingCriterion::COLUMN_CREATED_AT,
SetSortingRequest::ALBUM_SORTING_ORDER_ATTRIBUTE => SortingCriterion::ASC,
SetSortingRequest::PHOTO_SORTING_ORDER_ATTRIBUTE => SortingCriterion::ASC,
])->assertStatus(204);

AccessControl::logout();
}

public function testSetSortingWithIllegalAlbumAttribute(): void
{
AccessControl::log_as_id(0);

$response = $this->postJson('/api/Settings::setSorting',
[
SetSortingRequest::ALBUM_SORTING_COLUMN_ATTRIBUTE => '123',
SetSortingRequest::PHOTO_SORTING_COLUMN_ATTRIBUTE => SortingCriterion::COLUMN_CREATED_AT,
SetSortingRequest::ALBUM_SORTING_ORDER_ATTRIBUTE => SortingCriterion::ASC,
SetSortingRequest::PHOTO_SORTING_ORDER_ATTRIBUTE => SortingCriterion::ASC,
]);

$response->assertStatus(422);
$response->assertSee('sorting albums column must be null or one out of');

AccessControl::logout();
}

public function testSetSortingWithIllegalPhotoAttribute(): void
{
AccessControl::log_as_id(0);

$response = $this->postJson('/api/Settings::setSorting',
[
SetSortingRequest::ALBUM_SORTING_COLUMN_ATTRIBUTE => SortingCriterion::COLUMN_CREATED_AT,
SetSortingRequest::PHOTO_SORTING_COLUMN_ATTRIBUTE => '123',
SetSortingRequest::ALBUM_SORTING_ORDER_ATTRIBUTE => SortingCriterion::ASC,
SetSortingRequest::PHOTO_SORTING_ORDER_ATTRIBUTE => SortingCriterion::ASC,
]);

$response->assertStatus(422);
$response->assertSee('sorting photos column must be null or one out of');

AccessControl::logout();
}

public function testSetSortingWithUnknownOrder(): void
{
AccessControl::log_as_id(0);

$response = $this->postJson('/api/Settings::setSorting',
[
SetSortingRequest::ALBUM_SORTING_COLUMN_ATTRIBUTE => SortingCriterion::COLUMN_CREATED_AT,
SetSortingRequest::PHOTO_SORTING_COLUMN_ATTRIBUTE => SortingCriterion::COLUMN_CREATED_AT,
SetSortingRequest::ALBUM_SORTING_ORDER_ATTRIBUTE => '123',
SetSortingRequest::PHOTO_SORTING_ORDER_ATTRIBUTE => '123',
]);

$response->assertStatus(422);
$response->assertSee('order must be either');

AccessControl::logout();
}
}
Loading