File: D:/HostingSpaces/slenders/slenders.nl/tests/Browser/DocumentUploaderTest.php
<?php
namespace Tests\Browser;
use App\Komma\Documents\Models\Document;
use App\Komma\Users\Models\KmsUserRole;
use App\Komma\Users\Models\KmsUser;
use Tests\Browser\Pages\KmsUsersSectionTestPage;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
class DocumentUploaderTest extends DuskTestCase
{
/**
* @group DocumentUploading
* @test
* @throws \Throwable
* @see \App\Komma\Shop\Tests\Browser\DocumentUploaderTest The shop document uploading test
*/
public function testUserImageUploadSavingAndDeleting()
{
//Create a sample user
/** @var KmsUser $user */
$user = factory(KmsUser::class)->create();
$user->role = KmsUserRole::SuperAdmin;
$user->save();
$this->assertInstanceOf(KmsUser::class, $user);
$this->assertDatabaseHas((new KmsUser())->getTable(), [
'id' => $user->id
]);
$this->browse(function (Browser $browser) use($user) {
$browser->loginAs($user, 'kms')
//Open the user section
->visit(new KmsUsersSectionTestPage())
->pause(1000)
//Search and open the user
->type('@entity_search_input', $user->first_name)
->assertSeeIn('@search-result-counter', '1')
->click('@found_search_item')
->assertInputValue('@TextField-username', $user->username);
//Get the count of uploaded documents before we are going to upload. And get the latest id
$countBeforeUpload = Document::count();
//Upload profile picture
$browser->waitUntil('document.querySelector(\'#Documents-user-wrapper > ul\').childElementCount == 0', null, 'No images should be uploaded yet.') //Make sure no image is uploaded yet
->attach('@Documents-user_file_catcher', base_path('tests/Uploads/flower_image.jpg')) //Give the document uploader an image
->waitUntil('document.querySelector(\'#Documents-user-wrapper > ul\').childElementCount == 1', null,'An image could not uploaded') //One image should be getting uploaded (not yet uploaded completely).
->waitUntil('document.querySelector(\'#Documents-user-wrapper > ul > li:nth-child(1) .thumb\').classList.contains(\'is-uploading\');', null, 'Uploading was not started') //Wait until the image is uploading
->waitUntil('document.querySelector(\'#Documents-user-wrapper > ul > li:nth-child(1) .thumb\').classList.contains(\'is-uploading\') == false', null, 'Uploading did not complete'); //Wait until the image is uploaded completely
//Check that the document was uploaded and that it is referred to correctly in the database
$countAfterUpload = Document::count();
/** @var Document $latestDocumentAfterUpload */
$latestDocumentAfterUpload = Document::latest()->first();
$this->assertEquals($countBeforeUpload + 1, $countAfterUpload);
$this->assertFileExists(public_path(substr($latestDocumentAfterUpload->file_system_path, 1)));
$this->assertEquals(0, $latestDocumentAfterUpload->documentable_id); //This field will only contain a correct id after saving
$this->assertEmpty($latestDocumentAfterUpload->documentable_type); //This field will only be filled after saving
//Save the user. The documents documentable id and documentable type fields should now refer to the user
$browser->click('@save_button');
$latestDocumentAfterUpload->refresh();
$this->assertEquals($user->id, $latestDocumentAfterUpload->documentable_id); //This field will only contain a correct id after saving
$this->assertEquals(KmsUser::class, $latestDocumentAfterUpload->documentable_type); //This field will only be filled after saving
//Delete the image
$browser->click('#Documents-user-wrapper > ul > li:nth-child(1) .delete')
->waitUntil('document.querySelector(\'#Documents-user-wrapper > ul > li:nth-child(1)\').classList.contains(\'deleted\');', null, 'The document was not marked as deleted')
->click('@save_button')
->waitUntil('document.querySelector(\'#Documents-user-wrapper > ul\').childElementCount == 0', null, 'The just deleted image was not deleted.'); //Make the image is deleted
$this->assertFileNotExists(public_path(substr($latestDocumentAfterUpload->file_system_path, 1)));
$this->assertNull(Document::find($latestDocumentAfterUpload->id));
});
}
}