Skip to content

Commit

Permalink
fix delete (#898)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Jan 31, 2021
1 parent 307eb72 commit 2071a87
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 60 deletions.
24 changes: 19 additions & 5 deletions app/Actions/Album/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,29 @@ public function do(string $albumIDs): bool

$albums = Album::whereIn('id', explode(',', $albumIDs))->get();

Schema::disableForeignKeyConstraints();
$sqlPhoto = Photo::leftJoin('albums', 'photos.album_id', '=', 'albums.id')
->select('photos.*');

foreach ($albums as $album) {
$no_error &= $album->predelete();
$sqlPhoto = $sqlPhoto->orWhere(fn ($q) => $q->where('albums._lft', '>=', $album->_lft)
->where('albums._rgt', '<=', $album->_rgt));
}

//! We break the tree (because delete() is broken see https://github.com/lazychaser/laravel-nestedset/issues/485)
Album::where('_lft', '>', $album->_lft)->where('_rgt', '<=', $album->_rgt)->delete();
$photos = $sqlPhoto->get();
foreach ($photos as $photo) {
$no_error &= $photo->predelete();
$no_error &= $photo->delete();
}

$no_error &= $album->delete();
$sql_delete = Album::query();

//! We break the tree (because delete() is broken see https://github.com/lazychaser/laravel-nestedset/issues/485)
Schema::disableForeignKeyConstraints();
foreach ($albums as $album) {
$sql_delete = $sql_delete->orWhere(fn ($q) => $q
->where('_lft', '>=', $album->_lft)->where('_rgt', '<=', $album->_rgt));
}
$sql_delete->delete();
Schema::enableForeignKeyConstraints();

//? We fix the tree :)
Expand Down
7 changes: 4 additions & 3 deletions tests/Feature/AlbumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Feature;

use AccessControl;
use Tests\Feature\Lib\AlbumsUnitTest;
use Tests\Feature\Lib\SessionUnitTest;
use Tests\TestCase;
Expand Down Expand Up @@ -31,7 +32,7 @@ public function testAddReadLogged()
$albums_tests = new AlbumsUnitTest($this);
$session_tests = new SessionUnitTest();

$session_tests->log_as_id(0);
AccessControl::log_as_id(0);

$albums_tests->get('recent', '', 'true');
$albums_tests->get('starred', '', 'true');
Expand Down Expand Up @@ -102,7 +103,7 @@ public function testAddReadLogged()
/*
* Because we don't know login and password we are just going to assumed we are logged in.
*/
$session_tests->log_as_id(0);
AccessControl::log_as_id(0);

/*
* Let's try to delete this album.
Expand All @@ -122,7 +123,7 @@ public function testTrueNegative()
$albums_tests = new AlbumsUnitTest($this);
$session_tests = new SessionUnitTest();

$session_tests->log_as_id(0);
AccessControl::log_as_id(0);

$albums_tests->set_description('-1', 'new description', 'false');
$albums_tests->set_public('-1', 1, 1, 1, 0, 1, 1, 'false');
Expand Down
7 changes: 3 additions & 4 deletions tests/Feature/DiagnosticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Tests\Feature;

use AccessControl;
use App\Models\Configs;
use Tests\Feature\Lib\SessionUnitTest;
use Tests\TestCase;

class DiagnosticsTest extends TestCase
Expand All @@ -20,8 +20,7 @@ public function testDiagnostics()
$response = $this->get('/Diagnostics');
$response->assertStatus(200); // code 200 something

$session_tests = new SessionUnitTest();
$session_tests->log_as_id(0);
AccessControl::log_as_id(0);

$response = $this->get('/Diagnostics');
$response->assertStatus(200); // code 200 something
Expand All @@ -36,6 +35,6 @@ public function testDiagnostics()

Configs::where('key', '=', 'lossless_optimization')->update(['value' => '1']);

$session_tests->logout($this);
AccessControl::logout();
}
}
7 changes: 3 additions & 4 deletions tests/Feature/GeoDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Tests\Feature;

use AccessControl;
use App\Models\Configs;
use Illuminate\Http\UploadedFile;
use Tests\Feature\Lib\AlbumsUnitTest;
use Tests\Feature\Lib\PhotosUnitTest;
use Tests\Feature\Lib\SessionUnitTest;
use Tests\TestCase;

class GeoDataTest extends TestCase
Expand All @@ -18,9 +18,8 @@ public function testGeo()
{
$photos_tests = new PhotosUnitTest($this);
$albums_tests = new AlbumsUnitTest($this);
$session_tests = new SessionUnitTest();

$session_tests->log_as_id(0);
AccessControl::log_as_id(0);

/*
* Make a copy of the image because import deletes the file and we want to be
Expand Down Expand Up @@ -118,6 +117,6 @@ public function testGeo()
// reset
Configs::set('map_display', $map_display_value);

$session_tests->logout($this);
AccessControl::logout();
}
}
2 changes: 2 additions & 0 deletions tests/Feature/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature;

use AccessControl;
use App\Models\Configs;
use Tests\TestCase;

Expand Down Expand Up @@ -34,6 +35,7 @@ public function testHome()
*/
public function testPhpInfo()
{
AccessControl::logout();
// we don't want a non admin to access this
$response = $this->get('/phpinfo');
$response->assertStatus(200);
Expand Down
10 changes: 0 additions & 10 deletions tests/Feature/Lib/SessionUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Tests\Feature\Lib;

use App\ModelFunctions\SessionFunctions;
use Illuminate\Testing\TestResponse;
use Tests\TestCase;

Expand Down Expand Up @@ -110,13 +109,4 @@ public function set_old(
$response->assertOk();
$response->assertSee($result, false);
}

/**
* @param int $id
*/
public function log_as_id(int $id)
{
$sessionFunctions = new SessionFunctions();
$sessionFunctions->log_as_id($id);
}
}
13 changes: 5 additions & 8 deletions tests/Feature/LogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Tests\Feature;

use AccessControl;
use App\Models\Logs;
use Tests\Feature\Lib\SessionUnitTest;
use Tests\TestCase;

class LogsTest extends TestCase
Expand All @@ -17,22 +17,20 @@ class LogsTest extends TestCase
*/
public function testLogs()
{
$session_tests = new SessionUnitTest();

$response = $this->get('/Logs');
$response->assertOk();
$response->assertSeeText('false');

// set user as admin
$session_tests->log_as_id(0);
AccessControl::log_as_id(0);

Logs::notice(__METHOD__, __LINE__, 'test');
$response = $this->get('/Logs');
$response->assertOk();
$response->assertDontSeeText('false');
$response->assertViewIs('logs.list');

$session_tests->logout($this);
AccessControl::logout();
}

public function testApiLogs()
Expand All @@ -54,8 +52,7 @@ public function testClearLogs()
$response->assertSeeText('false');

// set user as admin
$session_tests = new SessionUnitTest();
$session_tests->log_as_id(0);
AccessControl::log_as_id(0);

$response = $this->post('/api/Logs::clearNoise');
$response->assertOk();
Expand All @@ -69,6 +66,6 @@ public function testClearLogs()
$response->assertOk();
$response->assertSeeText('Everything looks fine, Lychee has not reported any problems!');

$session_tests->logout($this);
AccessControl::logout();
}
}
7 changes: 3 additions & 4 deletions tests/Feature/PhotosRotateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Tests\Feature;

use AccessControl;
use App\Models\Configs;
use Illuminate\Http\UploadedFile;
use Tests\Feature\Lib\PhotosUnitTest;
use Tests\Feature\Lib\SessionUnitTest;
use Tests\TestCase;

class PhotosRotateTest extends TestCase
Expand All @@ -16,9 +16,8 @@ class PhotosRotateTest extends TestCase
public function testRotate()
{
$photos_tests = new PhotosUnitTest($this);
$session_tests = new SessionUnitTest();

$session_tests->log_as_id(0);
AccessControl::log_as_id(0);

/*
* Make a copy of the image because import deletes the file and we want to be
Expand Down Expand Up @@ -98,6 +97,6 @@ public function testRotate()
// reset
Configs::set('editor_enabled', $editor_enabled_value);

$session_tests->logout($this);
AccessControl::logout();
}
}
17 changes: 7 additions & 10 deletions tests/Feature/PhotosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Tests\Feature;

use AccessControl;
use App\Models\Configs;
use App\Models\Photo;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection as BaseCollection;
use Tests\Feature\Lib\AlbumsUnitTest;
use Tests\Feature\Lib\PhotosUnitTest;
use Tests\Feature\Lib\SessionUnitTest;
use Tests\TestCase;

class PhotosTest extends TestCase
Expand All @@ -22,9 +22,8 @@ public function testUpload()
{
$photos_tests = new PhotosUnitTest($this);
$albums_tests = new AlbumsUnitTest($this);
$session_tests = new SessionUnitTest();

$session_tests->log_as_id(0);
AccessControl::log_as_id(0);

/*
* Make a copy of the image because import deletes the file and we want to be
Expand Down Expand Up @@ -192,15 +191,14 @@ public function testUpload()
$response->assertOk();
$response->assertSee('true');

$session_tests->logout($this);
AccessControl::logout();
}

public function testTrueNegative()
{
$photos_tests = new PhotosUnitTest($this);
$session_tests = new SessionUnitTest();

$session_tests->log_as_id(0);
AccessControl::log_as_id(0);

$photos_tests->wrong_upload($this);
$photos_tests->wrong_upload2($this);
Expand All @@ -210,7 +208,7 @@ public function testTrueNegative()
$photos_tests->set_album('-1', '-1', 'false');
$photos_tests->set_license('-1', 'CC0', 'false');

$session_tests->logout($this);
AccessControl::logout();
}

public function testUpload2()
Expand All @@ -237,9 +235,8 @@ public function testImport()
{
$photos_tests = new PhotosUnitTest($this);
$albums_tests = new AlbumsUnitTest($this);
$session_tests = new SessionUnitTest();

$session_tests->log_as_id(0);
AccessControl::log_as_id(0);

// save initial value
$init_config_value = Configs::get_value('import_via_symlink');
Expand Down Expand Up @@ -270,6 +267,6 @@ public function testImport()
// set back to initial value
Configs::set('import_via_symlink', $init_config_value);

$session_tests->logout($this);
AccessControl::logout();
}
}
7 changes: 4 additions & 3 deletions tests/Feature/RSSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Tests\Feature;

use AccessControl;
use App\Models\Configs;
use Illuminate\Http\UploadedFile;
use Tests\Feature\Lib\AlbumsUnitTest;
use Tests\Feature\Lib\PhotosUnitTest;
use Tests\Feature\Lib\SessionUnitTest;
use Tests\TestCase;

class RSSTest extends TestCase
Expand Down Expand Up @@ -45,10 +45,9 @@ public function testRSS1()
// now we start adding some stuff
$photos_tests = new PhotosUnitTest($this);
$albums_tests = new AlbumsUnitTest($this);
$session_tests = new SessionUnitTest();

// log as admin
$session_tests->log_as_id(0);
AccessControl::log_as_id(0);

// create an album
$albumID = $albums_tests->add('0', 'test_album', 'true');
Expand Down Expand Up @@ -86,5 +85,7 @@ public function testRSS1()

Configs::set('Mod_Frame', $init_config_value);
Configs::set('full_photo', $init_full_photo);

AccessControl::logout();
}
}
7 changes: 3 additions & 4 deletions tests/Feature/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Tests\Feature;

use AccessControl;
use App\Models\Configs;
use Tests\Feature\Lib\SessionUnitTest;
use Tests\TestCase;

class UpdateTest extends TestCase
Expand Down Expand Up @@ -34,8 +34,7 @@ public function testDoLogged()
{
$gitpull = Configs::get_value('allow_online_git_pull', '0');

$session_tests = new SessionUnitTest();
$session_tests->log_as_id(0);
AccessControl::log_as_id(0);

Configs::set('allow_online_git_pull', '0');
$this->do_call('Error: Online updates are not allowed.');
Expand All @@ -53,6 +52,6 @@ public function testDoLogged()

Configs::set('allow_online_git_pull', $gitpull);

$session_tests->logout($this);
AccessControl::logout();
}
}
Loading

0 comments on commit 2071a87

Please sign in to comment.