Skip to content

Commit

Permalink
Start to build some module tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro authored and ifox committed Nov 18, 2019
1 parent 6a92f46 commit 6c59927
Show file tree
Hide file tree
Showing 16 changed files with 531 additions and 99 deletions.
4 changes: 2 additions & 2 deletions tests/helpers.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

function stubs($dir)
function stubs($dir = null)
{
return __DIR__ . "/stubs/{$dir}";
return __DIR__ . '/stubs' . ($dir ? "/{$dir}" : '');
}

function read_file($path)
Expand Down
64 changes: 20 additions & 44 deletions tests/integration/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,31 @@ class LoginTest extends TestCase
{
public function testCanRedirectToLogin()
{
$crawler = $this->followingRedirects()->call('GET', '/twill');
$this->request('/twill')->assertStatus(200);

$this->assertSame('http://twill.test/twill/login', url()->full());

$this->assertStringContainsString(
'Forgot password',
$crawler->getContent()
);

$crawler->assertStatus(200);
$this->assertStringContainsString('Forgot password', $this->content());
}

public function testCanLogin()
{
$crawler = $this->login();
$this->login();

$this->assertStringContainsString(
'Media Library',
$crawler->getContent()
);
$this->assertStringContainsString('Media Library', $this->content());

$this->assertStringContainsString('Settings', $crawler->getContent());
$this->assertStringContainsString('Settings', $this->content());

$this->assertStringContainsString('Logout', $crawler->getContent());
$this->assertStringContainsString('Logout', $this->content());
}

public function testCanLogout()
{
$this->login();

$crawler = $this->followingRedirects()->call('GET', '/twill/logout');
$this->request('/twill/logout');

$this->assertStringContainsString(
'Forgot password',
$crawler->getContent()
);
$this->assertStringContainsString('Forgot password', $this->content());
}

public function testGoogle2FA()
Expand All @@ -55,41 +44,28 @@ public function testGoogle2FA()

$user->update(['google_2fa_enabled' => true]);

$crawler = $this->login();
$this->login();

$this->assertStringContainsString(
'One-time password',
$crawler->getContent()
$this->content()
);

$crawler = $this->followingRedirects()->call(
'POST',
'/twill/login-2fa',
[
'verify-code' => 'INVALID CODE',
]
);
$this->request('/twill/login-2fa', 'POST', [
'verify-code' => 'INVALID CODE',
]);

$this->assertStringContainsString(
'Your one time password is invalid.',
$crawler->getContent()
$this->content()
);

$crawler = $this->followingRedirects()->call(
'POST',
'/twill/login-2fa',
[
'verify-code' => (new Google2FA())->getCurrentOtp(
$user->google_2fa_secret
),
]
);
$this->request('/twill/login-2fa', 'POST', [
'verify-code' => (new Google2FA())->getCurrentOtp(
$user->google_2fa_secret
),
])->assertStatus(200);

$crawler->assertStatus(200);

$this->assertStringContainsString(
'Media Library',
$crawler->getContent()
);
$this->assertStringContainsString('Media Library', $this->content());
}
}
16 changes: 7 additions & 9 deletions tests/integration/MediaLibraryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ public function setUp(): void

public function testCanListMedias()
{
$crawler = $this->ajax('/twill/media-library/medias', 'GET', [
$this->ajax('/twill/media-library/medias', 'GET', [
'page' => 1,
'type' => 'image',
'except' => [-1],
'search' => '*',
'tag' => '',
]);
])->assertStatus(200);

$crawler->assertStatus(200);

$this->assertJson($crawler->getContent());
$this->assertJson($this->content());
}

public function createMedia()
Expand All @@ -44,11 +42,11 @@ public function createMedia()
'qqfile' => UploadedFile::fake()->image($fileName),
];

$crawler = $this->ajax('/twill/media-library/medias', 'POST', $data);

$crawler->assertStatus(200);
$this->ajax('/twill/media-library/medias', 'POST', $data)->assertStatus(
200
);

$this->assertJson($crawler->getContent());
$this->assertJson($this->content());

$media = Media::where('filename', $fileName)->first();

Expand Down
68 changes: 68 additions & 0 deletions tests/integration/ModulesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace A17\Twill\Tests\Integration;

use Illuminate\Support\Facades\Schema;

class ModulesTest extends TestCase
{
private $authorFiles = [
'{$stubs}/modules/authors/2019_10_18_193753_create_authors_tables.php' =>
'{$database}/migrations/2019_10_18_193753_create_authors_tables.php',

'{$stubs}/modules/authors/admin.php' => '{$base}/routes/admin.php',

'{$stubs}/modules/authors/Author.php' => '{$app}/Models/Author.php',

'{$stubs}/modules/authors/AuthorController.php' =>
'{$app}/Http/Controllers/Admin/AuthorController.php',

'{$stubs}/modules/authors/AuthorRepository.php' =>
'{$app}/Repositories/AuthorRepository.php',

'{$stubs}/modules/authors/AuthorRequest.php' =>
'{$app}/Http/Requests/Admin/AuthorRequest.php',

'{$stubs}/modules/authors/form.blade.php' =>
'{$resources}/views/admin/authors/form.blade.php',

'{$stubs}/modules/authors/translatable.php' =>
'{$config}/translatable.php',

'{$stubs}/modules/authors/twill-navigation.php' =>
'{$config}/twill-navigation.php',
];

public function setUp(): void
{
parent::setUp();

$this->copyFiles($this->authorFiles);

$this->migrate();

$this->login();
}

public function testCanCopyFiles()
{
collect($this->authorFiles)->each(function ($destination) {
$this->assertFileExists($this->makeFileName($destination));
});
}

public function testCanMigrateDatabase()
{
$this->assertTrue(Schema::hasTable('authors'));
$this->assertTrue(Schema::hasTable('author_translations'));
$this->assertTrue(Schema::hasTable('author_slugs'));
$this->assertTrue(Schema::hasTable('author_revisions'));
}

public function testCanDisplayModuleInNavigation()
{
$this->request('/twill');

$this->assertStringContainsString('Personnel', $this->content());
}
}
33 changes: 11 additions & 22 deletions tests/integration/PasswordsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,11 @@ protected function sendPasswordResetToAdmin()
false
);

$crawler = $this->request($resetUrl);
$this->request($resetUrl);

$this->assertStringContainsString(
'Reset password',
$crawler->getContent()
);
$this->assertStringContainsString('Reset password', $this->content());

$this->assertStringContainsString(
'Confirm password',
$crawler->getContent()
);
$this->assertStringContainsString('Confirm password', $this->content());
}

public function testNotificationsAreFaked()
Expand All @@ -62,36 +56,31 @@ public function testNotificationsAreFaked()

public function testCanShowPasswordResetForm()
{
$crawler = $this->request('/twill/password/reset');
$this->request('/twill/password/reset')->assertStatus(200);

$crawler->assertStatus(200);

$this->assertStringContainsString(
'Reset password',
$crawler->getContent()
);
$this->assertStringContainsString('Reset password', $this->content());

$this->assertStringContainsString(
'Send password reset link',
$crawler->getContent()
$this->content()
);
}

public function testCanResetPassword()
{
$this->sendPasswordResetToAdmin();

$crawler = $this->request('/twill/password/reset', 'POST', [
$this->request('/twill/password/reset', 'POST', [
'_token' => csrf_token(),
'email' => $this->superAdmin()->email,
'password' => ($password = $this->faker->password(50)),
'password_confirmation' => $password,
'token' => Notification::token(),
]);
])->assertStatus(200);

$this->assertStringContainsString(
'Your password has been reset!',
$crawler->getContent()
$this->content()
);
}

Expand All @@ -101,7 +90,7 @@ public function testCanExpireResetPasswordToken()

DB::table('twill_password_resets')->truncate();

$crawler = $this->request('/twill/password/reset', 'POST', [
$this->request('/twill/password/reset', 'POST', [
'_token' => csrf_token(),
'email' => $this->superAdmin()->email,
'password' => ($password = $this->faker->password(50)),
Expand All @@ -111,7 +100,7 @@ public function testCanExpireResetPasswordToken()

$this->assertStringContainsString(
'Your password reset token has expired or could not be found, please retry.',
$crawler->getContent()
$this->content()
);
}
}
Loading

0 comments on commit 6c59927

Please sign in to comment.