|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Acquia\DFExtension\Context; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Component\Utility\Bytes; |
|
6
|
|
|
use Drupal\DrupalExtension\Context\DrupalSubContextBase; |
|
7
|
|
|
use Drupal\field\Entity\FieldConfig; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Contains steps for working with fields at the API level. |
|
11
|
|
|
*/ |
|
12
|
|
|
class FieldApiContext extends DrupalSubContextBase { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Sets the maximum upload size for a file or image field. |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $field |
|
18
|
|
|
* The field config entity ID. |
|
19
|
|
|
* @param string $size |
|
20
|
|
|
* (optional) The maximum upload size, e.g. 50 KB, 32 MB, etc. |
|
21
|
|
|
* |
|
22
|
|
|
* @Given :field has a maximum upload size of :size |
|
23
|
|
|
* @Given :field has no maximum upload size |
|
24
|
|
|
* |
|
25
|
|
|
* @When I set the maximum upload size of :field to :size |
|
26
|
|
|
* @When I clear the maximum upload size of :field |
|
27
|
|
|
*/ |
|
28
|
|
|
public function setMaximumUploadSize($field, $size = NULL) { |
|
29
|
|
|
$field = FieldConfig::load($field); |
|
30
|
|
|
|
|
31
|
|
|
/** @var UndoContext $undo */ |
|
32
|
|
|
$undo = $this->getContext(UndoContext::class); |
|
33
|
|
|
if ($undo) { |
|
34
|
|
|
$undo->push([$this, __FUNCTION__], [ |
|
35
|
|
|
$field->id(), |
|
36
|
|
|
$field->getSetting('max_filesize'), |
|
37
|
|
|
]); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($size) { |
|
|
|
|
|
|
41
|
|
|
$size = Bytes::toInt($size); |
|
42
|
|
|
} |
|
43
|
|
|
$field->setSetting('max_filesize', $size)->save(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Sets the allowed extensions for a file or image field. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $field |
|
50
|
|
|
* The field config entity ID. |
|
51
|
|
|
* @param string $extensions |
|
52
|
|
|
* A space-separated list of allowed extensions. |
|
53
|
|
|
* |
|
54
|
|
|
* @Given :field accepts the :extensions extension(s) |
|
55
|
|
|
* @Given :field accepts :extensions files |
|
56
|
|
|
* |
|
57
|
|
|
* @When I configure :field to accept the :extensions extension(s) |
|
58
|
|
|
* @When I configure :field to accept :extensions files |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setAllowedExtensions($field, $extensions) { |
|
61
|
|
|
$field = FieldConfig::load($field); |
|
62
|
|
|
|
|
63
|
|
|
/** @var UndoContext $undo */ |
|
64
|
|
|
$undo = $this->getContext(UndoContext::class); |
|
65
|
|
|
if ($undo) { |
|
66
|
|
|
$undo->push([$this, __FUNCTION__], [ |
|
67
|
|
|
$field->id(), |
|
68
|
|
|
$field->getSetting('file_extensions'), |
|
69
|
|
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$field->setSetting('file_extensions', $extensions)->save(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: