mirror of
https://github.com/Kovah/LinkAce.git
synced 2025-04-14 03:32:01 +02:00
Migrate link private status to visibility status (#165)
This commit is contained in:
parent
59bf073081
commit
92188bce89
@ -23,10 +23,10 @@ class SetDefaultSettingsForUser extends SettingsMigration
|
||||
$this->migrator->add($group . '.time_format', $defaults['time_format']);
|
||||
$this->migrator->add($group . '.locale', $defaults['locale']);
|
||||
|
||||
$this->migrator->add($group . '.links_private_default', $defaults['links_private_default']);
|
||||
$this->migrator->add($group . '.notes_private_default', $defaults['notes_private_default']);
|
||||
$this->migrator->add($group . '.lists_private_default', $defaults['lists_private_default']);
|
||||
$this->migrator->add($group . '.tags_private_default', $defaults['tags_private_default']);
|
||||
$this->migrator->add($group . '.links_default_visibility', $defaults['links_default_visibility']);
|
||||
$this->migrator->add($group . '.notes_default_visibility', $defaults['notes_default_visibility']);
|
||||
$this->migrator->add($group . '.lists_default_visibility', $defaults['lists_default_visibility']);
|
||||
$this->migrator->add($group . '.tags_default_visibility', $defaults['tags_default_visibility']);
|
||||
|
||||
$this->migrator->add($group . '.archive_backups_enabled', $defaults['archive_backups_enabled']);
|
||||
$this->migrator->add($group . '.archive_private_backups_enabled', $defaults['archive_private_backups_enabled']);
|
||||
|
17
app/Audits/Modifiers/VisibilityModifier.php
Normal file
17
app/Audits/Modifiers/VisibilityModifier.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Audits\Modifiers;
|
||||
|
||||
use App\Enums\ModelAttribute;
|
||||
|
||||
class VisibilityModifier implements ModifierInterface
|
||||
{
|
||||
public function modify($value): string
|
||||
{
|
||||
return match ($value) {
|
||||
ModelAttribute::VISIBILITY_PUBLIC => trans('attributes.visibility.' . ModelAttribute::VISIBILITY_PUBLIC),
|
||||
ModelAttribute::VISIBILITY_INTERNAL => trans('attributes.visibility.' . ModelAttribute::VISIBILITY_INTERNAL),
|
||||
ModelAttribute::VISIBILITY_PRIVATE => trans('attributes.visibility.' . ModelAttribute::VISIBILITY_PRIVATE),
|
||||
};
|
||||
}
|
||||
}
|
10
app/Enums/ModelAttribute.php
Normal file
10
app/Enums/ModelAttribute.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
class ModelAttribute
|
||||
{
|
||||
public const VISIBILITY_PUBLIC = 1;
|
||||
public const VISIBILITY_INTERNAL = 2;
|
||||
public const VISIBILITY_PRIVATE = 3;
|
||||
}
|
@ -8,7 +8,6 @@ use App\Settings\SystemSettings;
|
||||
use App\Settings\UserSettings;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
@ -44,9 +44,9 @@ class LinkStoreRequest extends FormRequest
|
||||
'tags' => [
|
||||
'nullable',
|
||||
],
|
||||
'is_private' => [
|
||||
'visibility' => [
|
||||
'sometimes',
|
||||
'boolean',
|
||||
'integer',
|
||||
],
|
||||
'check_disabled' => [
|
||||
'sometimes',
|
||||
|
@ -39,7 +39,7 @@ class LinkUpdateRequest extends FormRequest
|
||||
'description' => 'nullable|string',
|
||||
'lists' => 'nullable',
|
||||
'tags' => 'nullable',
|
||||
'is_private' => 'sometimes|boolean',
|
||||
'visibility' => 'sometimes|integer',
|
||||
'check_disabled' => 'sometimes|boolean',
|
||||
];
|
||||
|
||||
|
@ -6,6 +6,8 @@ use App\Audits\Modifiers\BooleanModifier;
|
||||
use App\Audits\Modifiers\LinkStatusModifier;
|
||||
use App\Audits\Modifiers\ListRelationModifier;
|
||||
use App\Audits\Modifiers\TagRelationModifier;
|
||||
use App\Audits\Modifiers\VisibilityModifier;
|
||||
use App\Enums\ModelAttribute;
|
||||
use App\Jobs\SaveLinkToWaybackmachine;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
@ -44,6 +46,7 @@ use OwenIt\Auditing\Contracts\Auditable;
|
||||
* @property BelongsTo $user
|
||||
* @method static Builder|Link byUser(int $user_id = null)
|
||||
* @method static Builder|Link privateOnly()
|
||||
* @method static Builder|Link internalOnly()
|
||||
* @method static Builder|Link publicOnly()
|
||||
*/
|
||||
class Link extends Model implements Auditable
|
||||
@ -58,7 +61,7 @@ class Link extends Model implements Auditable
|
||||
'title',
|
||||
'description',
|
||||
'icon',
|
||||
'is_private',
|
||||
'visibility',
|
||||
'status',
|
||||
'check_disabled',
|
||||
'thumbnail',
|
||||
@ -66,7 +69,7 @@ class Link extends Model implements Auditable
|
||||
|
||||
protected $casts = [
|
||||
'user_id' => 'integer',
|
||||
'is_private' => 'boolean',
|
||||
'visibility' => 'integer',
|
||||
'status' => 'integer',
|
||||
'check_disabled' => 'boolean',
|
||||
];
|
||||
@ -94,7 +97,7 @@ class Link extends Model implements Auditable
|
||||
];
|
||||
|
||||
public array $auditModifiers = [
|
||||
'is_private' => BooleanModifier::class,
|
||||
'visibility' => VisibilityModifier::class,
|
||||
'check_disabled' => BooleanModifier::class,
|
||||
'status' => LinkStatusModifier::class,
|
||||
self::AUDIT_TAGS_NAME => TagRelationModifier::class,
|
||||
@ -107,7 +110,8 @@ class Link extends Model implements Auditable
|
||||
*/
|
||||
|
||||
/**
|
||||
* Scope for the user relation
|
||||
* Scope for the user relation. If a user is specified, it will be used for
|
||||
* the scope. Otherwise, the currently authenticated user will be used.
|
||||
*
|
||||
* @param Builder $query
|
||||
* @param int|null $user_id
|
||||
@ -121,26 +125,19 @@ class Link extends Model implements Auditable
|
||||
return $query->where('user_id', $user_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope for the user relation
|
||||
*
|
||||
* @param Builder $query
|
||||
* @return Builder
|
||||
*/
|
||||
public function scopePrivateOnly(Builder $query): Builder
|
||||
{
|
||||
return $query->where('is_private', true);
|
||||
return $query->where('visibility', ModelAttribute::VISIBILITY_PRIVATE);
|
||||
}
|
||||
|
||||
public function scopeInternalOnly(Builder $query): Builder
|
||||
{
|
||||
return $query->where('visibility', ModelAttribute::VISIBILITY_INTERNAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope for the user relation
|
||||
*
|
||||
* @param Builder $query
|
||||
* @return Builder
|
||||
*/
|
||||
public function scopePublicOnly(Builder $query): Builder
|
||||
{
|
||||
return $query->where('is_private', false);
|
||||
return $query->where('visibility', ModelAttribute::VISIBILITY_PUBLIC);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Settings;
|
||||
|
||||
use App\Enums\ModelAttribute;
|
||||
use Spatie\LaravelSettings\Settings;
|
||||
|
||||
class UserSettings extends Settings
|
||||
@ -11,10 +12,10 @@ class UserSettings extends Settings
|
||||
public string $time_format;
|
||||
public string $locale;
|
||||
|
||||
public bool $links_private_default;
|
||||
public bool $notes_private_default;
|
||||
public bool $lists_private_default;
|
||||
public bool $tags_private_default;
|
||||
public int $links_default_visibility;
|
||||
public int $notes_default_visibility;
|
||||
public int $lists_default_visibility;
|
||||
public int $tags_default_visibility;
|
||||
|
||||
public bool $archive_backups_enabled;
|
||||
public bool $archive_private_backups_enabled;
|
||||
@ -63,10 +64,10 @@ class UserSettings extends Settings
|
||||
'date_format' => config('linkace.default.date_format'),
|
||||
'time_format' => config('linkace.default.time_format'),
|
||||
'locale' => config('app.fallback_locale'),
|
||||
'links_private_default' => false,
|
||||
'notes_private_default' => false,
|
||||
'lists_private_default' => false,
|
||||
'tags_private_default' => false,
|
||||
'links_default_visibility' => ModelAttribute::VISIBILITY_PUBLIC,
|
||||
'notes_default_visibility' => ModelAttribute::VISIBILITY_PUBLIC,
|
||||
'lists_default_visibility' => ModelAttribute::VISIBILITY_PUBLIC,
|
||||
'tags_default_visibility' => ModelAttribute::VISIBILITY_PUBLIC,
|
||||
'archive_backups_enabled' => true,
|
||||
'archive_private_backups_enabled' => true,
|
||||
'listitem_count' => 24,
|
||||
|
29
app/View/Components/Forms/VisibilityToggle.php
Normal file
29
app/View/Components/Forms/VisibilityToggle.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Components\Forms;
|
||||
|
||||
use App\Enums\ModelAttribute;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\View\Component;
|
||||
|
||||
class VisibilityToggle extends Component
|
||||
{
|
||||
public function render(): View
|
||||
{
|
||||
$public = ModelAttribute::VISIBILITY_PUBLIC;
|
||||
$internal = ModelAttribute::VISIBILITY_INTERNAL;
|
||||
$private = ModelAttribute::VISIBILITY_PRIVATE;
|
||||
|
||||
return view('components.forms.visibility-toggle', [
|
||||
'public' => $public,
|
||||
'internal' => $internal,
|
||||
'private' => $private,
|
||||
'publicSelected' => old('visibility') === $public
|
||||
|| (old('visibility') === null && usersettings('links_default_visibility') === $public),
|
||||
'internalSelected' => old('visibility') === $internal
|
||||
|| (old('visibility') === null && usersettings('links_default_visibility') === $internal),
|
||||
'privateSelected' => old('visibility') === $private
|
||||
|| (old('visibility') === null && usersettings('links_default_visibility') === $private),
|
||||
]);
|
||||
}
|
||||
}
|
@ -23,6 +23,7 @@
|
||||
"shaarli/netscape-bookmark-parser": "^v3.2",
|
||||
"spatie/laravel-activitylog": "^4.5",
|
||||
"spatie/laravel-backup": "^8.1.2",
|
||||
"spatie/laravel-permission": "^5.5",
|
||||
"spatie/laravel-settings": "^2.4",
|
||||
"symfony/http-client": "^6.0",
|
||||
"symfony/mailgun-mailer": "^6.0"
|
||||
|
84
composer.lock
generated
84
composer.lock
generated
@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "ac56bae7eab67688663b0a57b2d29ee3",
|
||||
"content-hash": "655f0861790ef7e6e5bcd7a30c246f6a",
|
||||
"packages": [
|
||||
{
|
||||
"name": "aws/aws-crt-php",
|
||||
@ -5464,6 +5464,88 @@
|
||||
],
|
||||
"time": "2022-03-15T20:01:36+00:00"
|
||||
},
|
||||
{
|
||||
"name": "spatie/laravel-permission",
|
||||
"version": "5.5.4",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/spatie/laravel-permission.git",
|
||||
"reference": "cb86fd87b43fcfc493c3f2b1de6fad100c078146"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/spatie/laravel-permission/zipball/cb86fd87b43fcfc493c3f2b1de6fad100c078146",
|
||||
"reference": "cb86fd87b43fcfc493c3f2b1de6fad100c078146",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"illuminate/auth": "^7.0|^8.0|^9.0",
|
||||
"illuminate/container": "^7.0|^8.0|^9.0",
|
||||
"illuminate/contracts": "^7.0|^8.0|^9.0",
|
||||
"illuminate/database": "^7.0|^8.0|^9.0",
|
||||
"php": "^7.3|^8.0|^8.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"orchestra/testbench": "^5.0|^6.0|^7.0",
|
||||
"phpunit/phpunit": "^9.4",
|
||||
"predis/predis": "^1.1"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"Spatie\\Permission\\PermissionServiceProvider"
|
||||
]
|
||||
},
|
||||
"branch-alias": {
|
||||
"dev-main": "5.x-dev",
|
||||
"dev-master": "5.x-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"files": [
|
||||
"src/helpers.php"
|
||||
],
|
||||
"psr-4": {
|
||||
"Spatie\\Permission\\": "src"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Freek Van der Herten",
|
||||
"email": "freek@spatie.be",
|
||||
"homepage": "https://spatie.be",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"description": "Permission handling for Laravel 6.0 and up",
|
||||
"homepage": "https://github.com/spatie/laravel-permission",
|
||||
"keywords": [
|
||||
"acl",
|
||||
"laravel",
|
||||
"permission",
|
||||
"permissions",
|
||||
"rbac",
|
||||
"roles",
|
||||
"security",
|
||||
"spatie"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/spatie/laravel-permission/issues",
|
||||
"source": "https://github.com/spatie/laravel-permission/tree/5.5.4"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://github.com/spatie",
|
||||
"type": "github"
|
||||
}
|
||||
],
|
||||
"time": "2022-05-16T12:09:59+00:00"
|
||||
},
|
||||
{
|
||||
"name": "spatie/laravel-settings",
|
||||
"version": "2.4.2",
|
||||
|
1
database/.gitignore
vendored
1
database/.gitignore
vendored
@ -1 +1,2 @@
|
||||
*.sqlite
|
||||
*.sqlite-journal
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Link;
|
||||
use App\Enums\ModelAttribute;
|
||||
use App\Models\User;
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
@ -24,7 +24,7 @@ class LinkFactory extends Factory
|
||||
? $this->faker->words(random_int(2, 5), true)
|
||||
: $this->faker->domainName(),
|
||||
'description' => $this->faker->boolean(70) ? $this->faker->sentences(random_int(1, 3), true) : null,
|
||||
'is_private' => $this->faker->boolean(10),
|
||||
'visibility' => ModelAttribute::VISIBILITY_PUBLIC,
|
||||
'check_disabled' => false,
|
||||
];
|
||||
}
|
||||
|
37
database/migrations/2022_06_23_112431_migrate_user_data.php
Normal file
37
database/migrations/2022_06_23_112431_migrate_user_data.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use App\Enums\ModelAttribute;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class MigrateUserData extends Migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
$this->migrateLinkVisibility();
|
||||
}
|
||||
|
||||
protected function migrateLinkVisibility(): void
|
||||
{
|
||||
Schema::table('links', function (Blueprint $table) {
|
||||
$table->integer('visibility')->default(ModelAttribute::VISIBILITY_PRIVATE)->after('is_private');
|
||||
});
|
||||
|
||||
$guestModeEnabled = systemsettings('guest_access_enabled');
|
||||
Link::query()->chunk(500, function ($links) use ($guestModeEnabled) {
|
||||
foreach ($links as $link) {
|
||||
$link->visibility = match ((bool)$link->is_private) {
|
||||
true => ModelAttribute::VISIBILITY_PRIVATE,
|
||||
false => $guestModeEnabled
|
||||
? ModelAttribute::VISIBILITY_PUBLIC : ModelAttribute::VISIBILITY_INTERNAL,
|
||||
};
|
||||
$link->saveQuietly();
|
||||
}
|
||||
});
|
||||
|
||||
Schema::table('links', function (Blueprint $table) {
|
||||
$table->dropColumn(['is_private']);
|
||||
});
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Enums\ModelAttribute;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Spatie\LaravelSettings\Migrations\SettingsMigration;
|
||||
|
||||
class MigrateExistingSettings extends SettingsMigration
|
||||
@ -80,20 +80,24 @@ class MigrateExistingSettings extends SettingsMigration
|
||||
);
|
||||
|
||||
$this->migrator->add(
|
||||
'user-1.links_private_default',
|
||||
(bool)$this->userSettings->get('links_private_default', false)
|
||||
'user-1.links_default_visibility',
|
||||
$this->userSettings->get('links_private_default', false)
|
||||
? ModelAttribute::VISIBILITY_PRIVATE : ModelAttribute::VISIBILITY_PUBLIC
|
||||
);
|
||||
$this->migrator->add(
|
||||
'user-1.notes_private_default',
|
||||
(bool)$this->userSettings->get('notes_private_default', false)
|
||||
'user-1.notes_default_visibility',
|
||||
$this->userSettings->get('notes_private_default', false)
|
||||
? ModelAttribute::VISIBILITY_PRIVATE : ModelAttribute::VISIBILITY_PUBLIC
|
||||
);
|
||||
$this->migrator->add(
|
||||
'user-1.lists_private_default',
|
||||
(bool)$this->userSettings->get('lists_private_default', false)
|
||||
'user-1.lists_default_visibility',
|
||||
$this->userSettings->get('lists_private_default', false)
|
||||
? ModelAttribute::VISIBILITY_PRIVATE : ModelAttribute::VISIBILITY_PUBLIC
|
||||
);
|
||||
$this->migrator->add(
|
||||
'user-1.tags_private_default',
|
||||
(bool)$this->userSettings->get('tags_private_default', false)
|
||||
'user-1.tags_default_visibility',
|
||||
$this->userSettings->get('tags_private_default', false)
|
||||
? ModelAttribute::VISIBILITY_PRIVATE : ModelAttribute::VISIBILITY_PUBLIC
|
||||
);
|
||||
|
||||
$this->migrator->add(
|
||||
|
11
lang/en_US/attributes.php
Normal file
11
lang/en_US/attributes.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
use App\Enums\ModelAttribute;
|
||||
|
||||
return [
|
||||
'visibility' => [
|
||||
ModelAttribute::VISIBILITY_PUBLIC => 'Public',
|
||||
ModelAttribute::VISIBILITY_INTERNAL => 'Internal',
|
||||
ModelAttribute::VISIBILITY_PRIVATE => 'Private',
|
||||
],
|
||||
];
|
@ -14,6 +14,8 @@ return [
|
||||
'update' => 'Update Link',
|
||||
'delete' => 'Delete Link',
|
||||
|
||||
'public' => 'Public Link',
|
||||
'internal' => 'Internal Link',
|
||||
'private' => 'Private Link',
|
||||
|
||||
'history_deleted' => 'Link was deleted',
|
||||
|
@ -35,8 +35,7 @@ return [
|
||||
|
||||
'continue_adding' => 'Continue Adding',
|
||||
|
||||
'private' => 'Private',
|
||||
'is_private' => 'Is private',
|
||||
'visibility' => 'Visibility',
|
||||
|
||||
'history' => 'History',
|
||||
'history_added' => 'Added <code>:newvalue</code> to :fieldname.',
|
||||
|
@ -16,5 +16,4 @@ return [
|
||||
|
||||
'two_factor_otp' => 'One Time Password',
|
||||
'two_factor_recovery_code' => 'Recovery Code',
|
||||
|
||||
];
|
||||
|
20
resources/views/components/forms/visibility-toggle.blade.php
Normal file
20
resources/views/components/forms/visibility-toggle.blade.php
Normal file
@ -0,0 +1,20 @@
|
||||
<div {{ $attributes }}>
|
||||
<label class="form-label" for="visibility">@lang('linkace.visibility')</label>
|
||||
<select id="visibility" name="visibility" class="form-select{{ $errors->has('visibility') ? ' is-invalid' : '' }}">
|
||||
<option value="{{ $public }}" {{ $publicSelected ? 'selected' : '' }}>
|
||||
@lang('attributes.visibility.' . $public)
|
||||
</option>
|
||||
<option value="{{ $internal }}" {{ $internalSelected ? 'selected' : '' }}>
|
||||
@lang('attributes.visibility.' . $internal)
|
||||
</option>
|
||||
<option value="{{ $private }}" {{ $privateSelected ? 'selected' : '' }}>
|
||||
@lang('attributes.visibility.' . $private)
|
||||
</option>
|
||||
</select>
|
||||
|
||||
@if ($errors->has('visibility'))
|
||||
<p class="invalid-feedback" role="alert">
|
||||
{{ $errors->first('visibility') }}
|
||||
</p>
|
||||
@endif
|
||||
</div>
|
@ -88,24 +88,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-4">
|
||||
<label class="form-label" for="is_private">@lang('linkace.is_private')</label>
|
||||
<select id="is_private" name="is_private"
|
||||
class="form-select{{ $errors->has('is_private') ? ' is-invalid' : '' }}">
|
||||
<option value="0">
|
||||
@lang('linkace.no')
|
||||
</option>
|
||||
<option value="1" @if(usersettings('links_private_default') === '1') selected @endif>
|
||||
@lang('linkace.yes')
|
||||
</option>
|
||||
</select>
|
||||
|
||||
@if ($errors->has('is_private'))
|
||||
<p class="invalid-feedback" role="alert">
|
||||
{{ $errors->first('is_private') }}
|
||||
</p>
|
||||
@endif
|
||||
</div>
|
||||
<x-forms.visibility-toggle class="mb-4"/>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
@ -7,7 +7,6 @@ use App\Models\Link;
|
||||
use App\Models\LinkList;
|
||||
use App\Models\Tag;
|
||||
use App\Models\User;
|
||||
use App\Settings\SettingsAudit;
|
||||
use App\Settings\UserSettings;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
@ -18,15 +17,12 @@ class LinkControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->user = User::factory()->create();
|
||||
$this->actingAs($this->user);
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user);
|
||||
|
||||
$testHtml = '<!DOCTYPE html><head>' .
|
||||
'<title>Example Title</title>' .
|
||||
@ -34,7 +30,7 @@ class LinkControllerTest extends TestCase
|
||||
'</head></html>';
|
||||
|
||||
Http::fake([
|
||||
'example.com' => Http::response($testHtml, 200),
|
||||
'example.com' => Http::response($testHtml),
|
||||
]);
|
||||
|
||||
Queue::fake();
|
||||
@ -83,7 +79,7 @@ class LinkControllerTest extends TestCase
|
||||
'description' => 'My custom description',
|
||||
'lists' => $list->name,
|
||||
'tags' => $tag->name,
|
||||
'is_private' => '1',
|
||||
'visibility' => 1,
|
||||
]);
|
||||
|
||||
$response->assertRedirect('links/1');
|
||||
@ -97,28 +93,6 @@ class LinkControllerTest extends TestCase
|
||||
$this->assertEquals($tag->name, $databaseLink->tags->first()->name);
|
||||
}
|
||||
|
||||
public function testStoreRequestWithPrivateDefault(): void
|
||||
{
|
||||
UserSettings::fake([
|
||||
'links_private_default' => true,
|
||||
]);
|
||||
|
||||
$response = $this->post('links', [
|
||||
'url' => 'https://example.com',
|
||||
'title' => null,
|
||||
'description' => null,
|
||||
'lists' => null,
|
||||
'tags' => null,
|
||||
'is_private' => usersettings('links_private_default'),
|
||||
]);
|
||||
|
||||
$response->assertRedirect('links/1');
|
||||
|
||||
$databaseLink = Link::first();
|
||||
|
||||
$this->assertTrue($databaseLink->is_private);
|
||||
}
|
||||
|
||||
public function testStoreRequestWithDuplicate(): void
|
||||
{
|
||||
Link::factory()->create([
|
||||
@ -131,7 +105,7 @@ class LinkControllerTest extends TestCase
|
||||
'description' => null,
|
||||
'lists' => null,
|
||||
'tags' => null,
|
||||
'is_private' => '0',
|
||||
'visibility' => 1,
|
||||
]);
|
||||
|
||||
$response->assertRedirect('links/2');
|
||||
@ -152,7 +126,7 @@ class LinkControllerTest extends TestCase
|
||||
'description' => null,
|
||||
'lists' => null,
|
||||
'tags' => null,
|
||||
'is_private' => '0',
|
||||
'visibility' => 1,
|
||||
]);
|
||||
|
||||
$response->assertRedirect('links/1');
|
||||
@ -166,7 +140,7 @@ class LinkControllerTest extends TestCase
|
||||
|
||||
public function testStoreRequestWithHugeThumbnail(): void
|
||||
{
|
||||
$img = 'https://assets.imgix.net/unsplash/unsplash006.jpg?w=640&h=400&usm=20&fit=crop&blend-mode=normal&blend-alpha=80&blend-x=30&blend-y=20&blend=https%3A%2F%2Fassets.imgix.net%2F~text%3Ftxt-color%3D9fb64d%26txt-font%3DAvenir%2BNext%2BHeavy%26txt-shad%3D20%26txt-size%3D32%26w%3D580%26txt%3Di%2Bthank%2Byou%2Bgod%2Bfor%2Bmost%2Bthis%2Bamazing%2Bday%3Afor%2Bthe%2Bleaping%2Bgreenly%2Bspirits%2Bof%2Btrees%2B-e.e.%2Bcummings';
|
||||
$img = 'https://picsum.photos/1000/500';
|
||||
|
||||
$testHtml = '<!DOCTYPE html><head>' .
|
||||
'<title>Example Title</title>' .
|
||||
@ -212,7 +186,7 @@ class LinkControllerTest extends TestCase
|
||||
'description' => null,
|
||||
'lists' => null,
|
||||
'tags' => null,
|
||||
'is_private' => '0',
|
||||
'visibility' => 1,
|
||||
]);
|
||||
|
||||
Queue::assertNotPushed(SaveLinkToWaybackmachine::class);
|
||||
@ -231,7 +205,7 @@ class LinkControllerTest extends TestCase
|
||||
'description' => null,
|
||||
'lists' => null,
|
||||
'tags' => null,
|
||||
'is_private' => '1',
|
||||
'visibility' => 1,
|
||||
]);
|
||||
|
||||
Queue::assertNotPushed(SaveLinkToWaybackmachine::class);
|
||||
@ -278,7 +252,7 @@ class LinkControllerTest extends TestCase
|
||||
'description' => 'New Description',
|
||||
'lists' => null,
|
||||
'tags' => null,
|
||||
'is_private' => '0',
|
||||
'visibility' => 1,
|
||||
'check_disabled' => '0',
|
||||
]);
|
||||
|
||||
@ -306,7 +280,7 @@ class LinkControllerTest extends TestCase
|
||||
'description' => 'New Description',
|
||||
'lists' => null,
|
||||
'tags' => null,
|
||||
'is_private' => '0',
|
||||
'visibility' => 1,
|
||||
]);
|
||||
|
||||
$response->assertNotFound();
|
||||
@ -324,7 +298,7 @@ class LinkControllerTest extends TestCase
|
||||
'description' => 'New Description',
|
||||
'lists' => null,
|
||||
'tags' => null,
|
||||
'is_private' => '0',
|
||||
'visibility' => 1,
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors([
|
||||
@ -343,7 +317,7 @@ class LinkControllerTest extends TestCase
|
||||
'description' => 'New Description',
|
||||
'lists' => null,
|
||||
'tags' => null,
|
||||
'is_private' => '0',
|
||||
'visibility' => 1,
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors([
|
||||
|
21
tests/Migrations/MigratesUpTo.php
Normal file
21
tests/Migrations/MigratesUpTo.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Migrations;
|
||||
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
|
||||
trait MigratesUpTo
|
||||
{
|
||||
protected function migrateUpTo(string $migration): void
|
||||
{
|
||||
$migrator = app('migrator');
|
||||
$dbPath = database_path('migrations');
|
||||
|
||||
$migrations = collect($migrator->getMigrationFiles($dbPath))
|
||||
->takeWhile(fn($file) => $file !== $dbPath . '/' . $migration);
|
||||
|
||||
$migrations->prepend(database_path('schema/sqlite-schema.dump'));
|
||||
|
||||
Artisan::call('migrate:fresh', ['--realpath' => 'true', '--path' => $migrations->values()]);
|
||||
}
|
||||
}
|
@ -14,7 +14,7 @@ use Tests\TestCase;
|
||||
|
||||
class RevisionsToAuditsMigrationTest extends TestCase
|
||||
{
|
||||
use LazilyRefreshDatabase;
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function afterRefreshingDatabase(): void
|
||||
{
|
||||
|
76
tests/Migrations/UserDataMigrationTest.php
Normal file
76
tests/Migrations/UserDataMigrationTest.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Migrations;
|
||||
|
||||
use App\Models\Link;
|
||||
use App\Settings\SystemSettings;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UserDataMigrationTest extends TestCase
|
||||
{
|
||||
use MigratesUpTo;
|
||||
|
||||
public function testLinkVisibilityMigration(): void
|
||||
{
|
||||
$this->migrateUpTo('2022_06_23_112431_migrate_user_data.php');
|
||||
|
||||
Link::unguard();
|
||||
Link::create([
|
||||
'url' => 'https://private-link.com',
|
||||
'title' => 'Test',
|
||||
'user_id' => 1,
|
||||
'is_private' => true,
|
||||
]);
|
||||
|
||||
Link::create([
|
||||
'url' => 'https://public-link.com',
|
||||
'title' => 'Test',
|
||||
'user_id' => 1,
|
||||
'is_private' => false,
|
||||
]);
|
||||
|
||||
$this->artisan('migrate');
|
||||
|
||||
$this->assertDatabaseHas('links', [
|
||||
'url' => 'https://private-link.com',
|
||||
'visibility' => 3, // is private
|
||||
]);
|
||||
$this->assertDatabaseHas('links', [
|
||||
'url' => 'https://public-link.com',
|
||||
'visibility' => 2, // is internal
|
||||
]);
|
||||
}
|
||||
|
||||
public function testLinkVisibilityMigrationWithEnabledGuestMode(): void
|
||||
{
|
||||
$this->migrateUpTo('2022_06_23_112431_migrate_user_data.php');
|
||||
|
||||
SystemSettings::fake(['guest_access_enabled' => true]);
|
||||
|
||||
Link::unguard();
|
||||
Link::create([
|
||||
'url' => 'https://private-link.com',
|
||||
'title' => 'Test',
|
||||
'user_id' => 1,
|
||||
'is_private' => true,
|
||||
]);
|
||||
|
||||
Link::create([
|
||||
'url' => 'https://public-link.com',
|
||||
'title' => 'Test',
|
||||
'user_id' => 1,
|
||||
'is_private' => false,
|
||||
]);
|
||||
|
||||
$this->artisan('migrate');
|
||||
|
||||
$this->assertDatabaseHas('links', [
|
||||
'url' => 'https://private-link.com',
|
||||
'visibility' => 3, // is private
|
||||
]);
|
||||
$this->assertDatabaseHas('links', [
|
||||
'url' => 'https://public-link.com',
|
||||
'visibility' => 1, // is public
|
||||
]);
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ namespace Tests;
|
||||
|
||||
use App\Settings\SystemSettings;
|
||||
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
@ -13,8 +14,10 @@ abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
SystemSettings::fake([
|
||||
'setup_completed' => true
|
||||
]);
|
||||
if (Schema::hasTable('settings')) {
|
||||
SystemSettings::fake([
|
||||
'setup_completed' => true,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user