Completed
Pull Request — master (#326)
by Damian
02:15
created

FeatureContext::iAttachTheFileToDropzone()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 37
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 1
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Tests\Behat\Context;
4
5
use Behat\Behat\Context\Step;
6
use SilverStripe\Framework\Test\Behaviour\FeatureContext as BaseFeatureContext;
7
8
class FeatureContext extends BaseFeatureContext
9
{
10
	/**
11
	 * Initializes context.
12
	 * Every scenario gets it's own context object.
13
	 *
14
	 * @param array $parameters Context parameters (set them up through behat.yml)
15
	 */
16
	public function __construct(array $parameters) {
17
		parent::__construct($parameters);
18
19
		// Override existing fixture context with more specific one
20
		$fixtureContext = new FixtureContext($parameters);
21
		$fixtureContext->setFixtureFactory($this->getFixtureFactory());
22
		$this->useContext('FixtureContext', $fixtureContext);
23
	}
24
25
    /**
26
     * @Given /^I attach the file "([^"]*)" to dropzone$/
27
     * @see MinkContext::attachFileToField()
28
     */
29
    public function iAttachTheFileToDropzone($path)
30
    {
31
        // Get path
32
        if ($this->getMinkParameter('files_path')) {
33
            $fullPath = rtrim(realpath($this->getMinkParameter('files_path')), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$path;
34
            if (is_file($fullPath)) {
35
                $path = $fullPath;
36
            }
37
        }
38
39
        // Find field
40
        $input = $this->getSession()->getPage()->find('css', 'input[type="file"].dz-hidden-input');
41
        assertNotNull($input, "Could not find dz-hidden-input");
42
43
        // Make visible temporarily while attaching
44
        $this->getSession()->executeScript(
45
<<<EOS
46
window.jQuery('.dz-hidden-input')
47
    .css('visibility', 'visible')
48
    .width(1)
49
    .height(1);
50
EOS
51
        );
52
53
        // Attach via html5
54
        $input->attachFile($path);
55
56
        // Restore hidden state
57
        $this->getSession()->executeScript(
58
<<<EOS
59
window.jQuery('.dz-hidden-input')
60
    .css('visibility', 'hidden')
61
    .width(0)
62
    .height(0);
63
EOS
64
        );
65
    }
66
}
67