Skip to content

Commit e0e9540

Browse files
committed
Fix tests for multitenancy
1 parent e53c0fa commit e0e9540

6 files changed

Lines changed: 41 additions & 29 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ resources/sass/tenant_*
4949
resources/sass/_variables_bk.scss
5050
devhub/pm-font/dist
5151
test-db-snapshot.db
52+
snapshot_*.db
5253
storage/transitions

‎tests/Feature/Api/TasksTest.php‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,7 @@ public function testGetListAssignedTasks()
158158
*/
159159
public function testGetListClosedTasks()
160160
{
161-
// Run the permission seeder
162-
(new PermissionSeeder)->run();
163-
164-
// Reboot our AuthServiceProvider. This is necessary so that it can
165-
// pick up the new permissions and setup gates for each of them.
166-
$asp = new AuthServiceProvider(app());
167-
$asp->boot();
161+
$this->initializePermissions();
168162

169163
$user_1 = User::factory()->create();
170164
$user_1->giveDirectPermission('view-all_requests');

‎tests/Feature/Shared/RequestHelper.php‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Tests\Feature\Shared;
44

5-
use Database\Seeders\PermissionSeeder;
65
use Illuminate\Support\Facades\Cache;
76
use Illuminate\Support\Facades\Hash;
87
use PHPUnit\Framework\TestStatus\Failure;
@@ -28,21 +27,9 @@ protected function setUp(): void
2827
'is_administrator' => true,
2928
]);
3029

31-
if ($this->withPermissions === true) {
32-
//Run the permission seeder
33-
(new PermissionSeeder)->run();
34-
35-
// Reboot our AuthServiceProvider. This is necessary so that it can
36-
// pick up the new permissions and setup gates for each of them.
37-
$asp = new AuthServiceProvider(app());
38-
$asp->boot();
39-
$asp->defineGates();
40-
}
41-
4230
if (method_exists($this, 'withUserSetup')) {
4331
$this->withUserSetup();
44-
$asp = new AuthServiceProvider(app());
45-
$asp->defineGates();
32+
$this->initializePermissions(false);
4633
}
4734
}
4835

‎tests/Feature/Upgrade/PopulateCasesParticipatedTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private function upgrade()
7979

8080
// This upgrade creates a table so we need to restore the database whenever this is run.
8181
$this->beforeApplicationDestroyed(function () {
82-
$this->restoreDatabaseFromSnapshot();
82+
$this->restoreDatabaseSnapshot('non-transactional-test');
8383
});
8484
}
8585

‎tests/Model/UserTest.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public function testPermissions()
6363

6464
public function testCanAnyFirst()
6565
{
66+
dump('Starting test');
67+
6668
$user = User::factory()->create();
6769

6870
$p1 = Permission::factory()->create(['name' => 'foo']);
@@ -72,7 +74,7 @@ public function testCanAnyFirst()
7274
Cache::forget('permissions');
7375
Cache::forget("user_{$user->id}_permissions");
7476

75-
(new AuthServiceProvider(app()))->boot();
77+
$this->initializePermissions(false);
7678

7779
$this->assertFalse($user->can('bar'));
7880
$this->assertFalse($user->canAnyFirst('foo|bar'));
@@ -103,7 +105,7 @@ public function testAddCategoryViewPermissions()
103105

104106
$perm = Permission::factory()->create(['name' => "{$method}-{$plural}"]);
105107

106-
(new AuthServiceProvider(app()))->boot();
108+
$this->initializePermissions(false);
107109

108110
$this->assertFalse($user->can($perm->name));
109111
$this->assertFalse($user->can($viewCatPerm->name));

‎tests/TestCase.php‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests;
44

55
use ArrayAccess;
6+
use Database\Seeders\PermissionSeeder;
67
use Illuminate\Foundation\Testing\RefreshDatabase;
78
use Illuminate\Foundation\Testing\RefreshDatabaseState;
89
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
@@ -19,6 +20,7 @@
1920
use ProcessMaker\Jobs\RefreshArtisanCaches;
2021
use ProcessMaker\Models\Process;
2122
use ProcessMaker\Models\User;
23+
use ProcessMaker\Providers\AuthServiceProvider;
2224
use Tests\TestSeeder;
2325

2426
abstract class TestCase extends BaseTestCase
@@ -54,7 +56,7 @@ protected function setUp(): void
5456

5557
if (self::$currentNonTransactionalTest && self::$currentNonTransactionalTest !== $class) {
5658
// The last test to run was non-transactional, so we need to refresh the database
57-
$this->restoreDatabaseFromSnapshot();
59+
$this->restoreDatabaseSnapshot('non-transactional-test');
5860
self::$currentNonTransactionalTest = null;
5961
}
6062

@@ -89,8 +91,26 @@ protected function setUp(): void
8991
}
9092

9193
if (!self::$databaseSnapshotFile) {
92-
self::$databaseSnapshotFile = $this->takeDatabaseSnapshot();
94+
self::$databaseSnapshotFile = $this->takeDatabaseSnapshot('non-transactional-test');
9395
}
96+
97+
if ($this->withPermissions === true) {
98+
$this->initializePermissions();
99+
}
100+
}
101+
102+
public function initializePermissions($runSeeder = true)
103+
{
104+
if ($runSeeder) {
105+
//Run the permission seeder
106+
(new PermissionSeeder)->run();
107+
}
108+
109+
// Reboot our AuthServiceProvider. This is necessary so that it can
110+
// pick up the new permissions and setup gates for each of them.
111+
$asp = new AuthServiceProvider(app());
112+
$asp->boot();
113+
$asp->defineGates();
94114
}
95115

96116
/**
@@ -217,12 +237,14 @@ private function populateDatabase() : bool
217237
return (bool) env('POPULATE_DATABASE', true);
218238
}
219239

220-
private function takeDatabaseSnapshot($filename = 'test-db-snapshot.db')
240+
public function takeDatabaseSnapshot($id = null)
221241
{
222242
if (!$this->populateDatabase()) {
223243
return;
224244
}
225245

246+
$filename = 'snapshot_' . $id . '.db';
247+
226248
$snapshotFile = base_path($filename);
227249
$command = 'mysqldump ' . $this->mysqlConnectionString();
228250
$command .= ' ' . env('DB_DATABASE') . ' > ' . $snapshotFile;
@@ -231,11 +253,17 @@ private function takeDatabaseSnapshot($filename = 'test-db-snapshot.db')
231253
dd("Failed to take database snapshot: $command");
232254
}
233255

234-
return $snapshotFile;
256+
return $id;
235257
}
236258

237-
public function restoreDatabaseFromSnapshot($filename = 'test-db-snapshot.db')
259+
public function restoreDatabaseSnapshot($id)
238260
{
261+
$filename = 'snapshot_' . $id . '.db';
262+
263+
if (!file_exists(base_path($filename))) {
264+
throw new \Exception("Database snapshot not found: $filename");
265+
}
266+
239267
if (!$this->populateDatabase()) {
240268
return;
241269
}

0 commit comments

Comments
 (0)