HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/stafa.komma.pro/app/Komma/Shop/Tests/Browser/DocumentUploaderTest.php
<?php

namespace App\Komma\Shop\Tests\Browser;

use App\Komma\Documents\Models\Document;
use App\Komma\Shop\Products\Product\Product;
use App\Komma\Shop\Tests\Browser\Pages\ProductsSectionTestPage;
use App\Komma\Users\Models\KmsUser;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;

class DocumentUploaderTest extends DuskTestCase
{
    /**
     * @group DocumentUploading
     * @test
     * @throws \Throwable
     * @see \Tests\Browser\DocumentUploaderTest The kms document uploading test
     */
    public function testImageUploadingAndSortingForProducts()
    {
        //And a sample product
        /** @var Product $product */
        $product = factory(Product::class)->create();
        $this->assertInstanceOf(Product::class, $product);

        $this->browse(function (Browser $browser) use($product) {
            $browser->loginAs(KmsUser::find(1), 'kms')
            //Open the product section
                ->visit(new ProductsSectionTestPage())

            //Search and open the product
                ->type('@entity_search_input', $product->getSidebarName())
                ->assertSeeIn('@search-result-counter', '1')
                ->click('@found_search_item');

            //Get the count of uploaded documents before we are going to upload. And get the latest id
            $countBeforeUpload = Document::count();

            $productImages = ['milling_cutter.jpg', 'screwdrivers.jpg', 'wrenches.jpg'];

            //Upload the images
            $browser->waitUntil('document.querySelector(\'#Documents-documents-wrapper > ul\').childElementCount == 0', null, 'No images should be uploaded yet.'); //Make sure no image is uploaded yet
            foreach($productImages as $index => $productImage) {
                $browser->scrollToElement('#Documents-documents-wrapper'); //See the extraDuskTools trait for the definition of this non native method

                //Upload picture
                $browser->attach('@Documents-documents_file_catcher', base_path('tests/Uploads/'.$productImage)) //Give the document uploader an image
                ->waitUntil('document.querySelector(\'#Documents-documents-wrapper > ul\').childElementCount == '.($index + 1), null,'An image could not uploaded') //One image should be getting uploaded (not yet uploaded completely).
                ->waitUntil('document.querySelector(\'#Documents-documents-wrapper > ul > li:nth-child('.($index + 1).') .thumb\').classList.contains(\'is-uploading\');', null, 'Uploading was not started') //Wait until the image is uploading
                ->waitUntil('document.querySelector(\'#Documents-documents-wrapper > ul > li:nth-child('.($index + 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 + $index + 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 product. The documents documentable id and documentable type fields should now refer to the product
            $browser->click('@save_button');
            $documentsAfterUpload = Document::latest()->orderBy('sort_order', 'asc')->limit(count($productImages))->get();

            $documentsAfterUpload->each(function(Document $latestDocument) use($product) {
                $this->assertEquals($product->id, $latestDocument->documentable_id); //This field will only contain a correct id after saving
                $this->assertEquals(Product::class, $latestDocument->documentable_type); //This field will only be filled after saving
            });

            $browser->scrollToElement('#Documents-documents-wrapper'); //See the extraDuskTools trait for the definition of this non native method
            $browser->pause(1000);

            //Re-arrange the documents. The first document in the array has sort order 1, the second 2, and the third has 3. We are going to sort them that their order will become, 2, 1, 3.
//            $browser->waitUntil('monitorEvents(document.querySelector("#Documents-documents_1 > span.drag-icon", ["mousedown","mousemove","dragstart","dragenter","dragover","drop","dragend","mouseup",]))');
//            $browser->drag('#Documents-documents_1 > span.drag-icon', '#Documents-documents_0 > span.drag-icon'); //NOTICE: This is not working somehow.
//            $browser->dragUp('#Documents-documents-wrapper > ul > li:nth-child(2) .drag-icon', 20); //NOTICE: This is also not working somehow.

            //Delete the images
            foreach($productImages as $index => $productImage) {
                $browser->click('#Documents-documents-wrapper > ul > li:nth-child('.($index + 1).') .delete')
                    ->waitUntil('document.querySelector(\'#Documents-documents-wrapper > ul > li:nth-child('.($index + 1).')\').classList.contains(\'deleted\');', null, 'The document was not marked as deleted');
            }
            $browser->click('@save_button');
            $documentsAfterUpload->each(function (Document $document) use($browser) {
                $browser->waitUntil('document.querySelector(\'#Documents-documents-wrapper > ul\').childElementCount == 0', null, 'The just deleted image was not deleted.');
                $this->assertFileNotExists(public_path(substr($document->file_system_path, 1)));
                $this->assertNull(Document::find($document->id));
            });
        });
    }
}