Skip to content

Commit

Permalink
Test real upload in Behat (#1783)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed May 9, 2022
1 parent 7ec9f44 commit c36f83d
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion demos/basic/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/** @var \Atk4\Ui\App $app */
require_once __DIR__ . '/../init-app.php';

$img = 'https://raw.githubusercontent.com/atk4/ui/2.0.4/public/logo.png';
$img = $app->cdn['atk'] . '/logo.png';

$seg = \Atk4\Ui\View::addTo($app, ['ui' => 'segment']);
\Atk4\Ui\Header::addTo($seg, ['H1 Header', 'size' => 1]);
Expand Down
2 changes: 1 addition & 1 deletion demos/basic/label.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/** @var \Atk4\Ui\App $app */
require_once __DIR__ . '/../init-app.php';

$img = 'https://raw.githubusercontent.com/atk4/ui/2.0.4/public/logo.png';
$img = $app->cdn['atk'] . '/logo.png';

\Atk4\Ui\Header::addTo($app, ['Labels']);
\Atk4\Ui\Label::addTo($app, ['Hot!']);
Expand Down
2 changes: 1 addition & 1 deletion demos/basic/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/** @var \Atk4\Ui\App $app */
require_once __DIR__ . '/../init-app.php';

$img = 'https://github.com/atk4/ui/raw/07208a0af84109f0d6e3553e242720d8aeedb784/public/logo.png';
$img = $app->cdn['atk'] . '/logo.png';

\Atk4\Ui\Header::addTo($app, ['Message Types']);

Expand Down
2 changes: 1 addition & 1 deletion demos/basic/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/** @var \Atk4\Ui\App $app */
require_once __DIR__ . '/../init-app.php';

$img = 'https://github.com/atk4/ui/raw/07208a0af84109f0d6e3553e242720d8aeedb784/public/logo.png';
$img = $app->cdn['atk'] . '/logo.png';

\Atk4\Ui\Header::addTo($app, ['Default view has no styling']);
\Atk4\Ui\View::addTo($app)->set('just a <div> element');
Expand Down
19 changes: 14 additions & 5 deletions demos/form-control/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$form = Form::addTo($app);
$img = $form->addControl('img', [Form\Control\UploadImage::class, ['defaultSrc' => '../images/default.png', 'placeholder' => 'Click to add an image.']]);
// $img->set('a_new_token', 'an-img-file-name');
// $img->setThumbnailSrc('./images/logo.png');
// $img->setThumbnailSrc($app->cdn['atk'] . '/logo.png');

$control = $form->addControl('file', [Form\Control\Upload::class, ['accept' => ['.png', '.jpg']]]);

Expand All @@ -34,7 +34,7 @@
return $form->error('img', 'Error uploading image.');
}

$img->setThumbnailSrc('./images/logo.png');
$img->setThumbnailSrc($img->getApp()->cdn['atk'] . '/logo.png');
$img->set('123456', $postFile['name'] . ' (token: 123456)');

// Do file processing here...
Expand Down Expand Up @@ -62,15 +62,24 @@
]);
});

$control->onUpload(function ($files) use ($form, $control) {
if ($files === 'error') {
$control->onUpload(function ($postFile) use ($form, $control) {
if ($postFile['error'] !== 0) {
return $form->error('file', 'Error uploading file.');
}
$control->setFileId('a_token');

$tmpFilePath = sys_get_temp_dir() . '/atk4-ui-upload-'
. hash('sha256', random_bytes(64) . microtime(true) . $postFile['tmp_name']) . '.bin';
try {
move_uploaded_file($postFile['tmp_name'], $tmpFilePath);
$data = file_get_contents($tmpFilePath);
} finally {
@unlink($tmpFilePath);
}

return new \Atk4\Ui\JsToast([
'title' => 'Upload success',
'message' => 'File is uploaded!',
'message' => 'File is uploaded! (name: ' . $postFile['name'] . ', md5: ' . md5($data) . ')',
'class' => 'success',
]);
});
Expand Down
2 changes: 1 addition & 1 deletion docs/header.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Header may specify icon or image::

Here you can also specify seed for the image::

$img = 'https://raw.githubusercontent.com/atk4/ui/2.0.4/public/logo.png';
$img = $app->cdn['atk'] . '/logo.png';
Header::addTo($seg, [
'Center-aligned header',
'aligned' => 'center',
Expand Down
2 changes: 1 addition & 1 deletion docs/image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ Specify classes

You can pass additional classes to an image::

$img = 'https://raw.githubusercontent.com/atk4/ui/2.0.4/public/logo.png';
$img = $app->cdn['atk'] . '/logo.png';
$icon = Image::addTo($app, [$img, 'disabled']);

2 changes: 1 addition & 1 deletion docs/label.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Image

Image cannot be specified at the same time with the icon, but you can use PNG/GIF/JPG image on your label::

$img = 'https://raw.githubusercontent.com/atk4/ui/2.0.4/public/logo.png';
$img = $app->cdn['atk'] . '/logo.png';
Label::addTo($app, ['Coded in PHP', 'image' => $img]);

Detail
Expand Down
2 changes: 1 addition & 1 deletion docs/template.rst
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ The tag {$_id} is automatically replaced with a unique name by a View.
There are more templates which are being substituted:

- {page}logout{/} - will be replaced with relative URL to the page
- {public}images/logo.png{/} - will replace with URL to a public asset
- {public}logo.png{/} - will replace with URL to a public asset
- {css}css/file.css{/} - will replace with URL link to a CSS file
- {js}jquery.validator.js{/} - will replace with URL to JavaScript file

Expand Down
14 changes: 14 additions & 0 deletions src/Behat/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,20 @@ public function iSelectValueInLookup(string $value, string $inputName): void
$this->jqueryWait('!$("#' . $lookupElem->getAttribute('id') . '").hasClass("visible")');
}

/**
* @When I select file input :arg1 with :arg2 as :arg3
*/
public function iSelectFile(string $inputName, string $fileContent, string $fileName): void
{
$element = $this->findElement(null, 'xpath(//input[@name="' . $inputName . '" and @type="hidden"]/following-sibling::input[@type="file"])');
$this->getSession()->executeScript(<<<'EOF'
const dataTransfer = new DataTransfer();
dataTransfer.items.add(new File([new Uint8Array(arguments[1])], arguments[2]));
arguments[0].files = dataTransfer.files;
$(arguments[0]).trigger('change');
EOF, [$element, array_map('ord', str_split($fileContent)), $fileName]);
}

/**
* Generic ScopeBuilder rule with select operator and input value.
*
Expand Down
9 changes: 9 additions & 0 deletions tests-behat/upload.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: Upload

Scenario:
Given I am on "form-control/upload.php"
When I select file input "file" with "Foo" as "bar.txt"
Then Toast display should contain text "(name: bar.txt, md5: 1356c67d7ad1638d816bfb822dd2c25d)"

When I select file input "file" with "Žlutý kůň" as "$kůň"
Then Toast display should contain text "(name: $kůň, md5: b047fb155be776f5bbae061c7b08cdf0)"

0 comments on commit c36f83d

Please sign in to comment.