Skip to content

Commit

Permalink
Add the unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Jul 21, 2017
1 parent 9bb55cf commit 0d83931
Show file tree
Hide file tree
Showing 13 changed files with 1,415 additions and 72 deletions.
1 change: 0 additions & 1 deletion src/Controller/BackendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ public function alertsAction()
*
* @return RedirectResponse
*
*
* @Route("/_contao/picker", name="contao_backend_picker")
*/
public function pickerAction(Request $request)
Expand Down
4 changes: 2 additions & 2 deletions src/Picker/AbstractPickerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ protected function getUser()

$user = $token->getUser();

if (null === $user) {
throw new \RuntimeException('The token does not contain a user');
if (!($user instanceof BackendUser)) {
throw new \RuntimeException('The token does not contain a back end user object');
}

return $user;
Expand Down
2 changes: 1 addition & 1 deletion src/Picker/ArticlePickerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function getDcaAttributes(PickerConfig $config)
{
$attributes = ['fieldType' => 'radio'];

if ('link' === $config->getContext() && $this->supportsValue($config)) {
if ($this->supportsValue($config)) {
$attributes['value'] = str_replace(['{{article_url::', '}}'], '', $config->getValue());
}

Expand Down
68 changes: 32 additions & 36 deletions src/Picker/FilePickerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,13 @@ public function supportsContext($context)
*/
public function supportsValue(PickerConfig $config)
{
if ('link' === $config->getContext()
&& (false !== strpos($config->getValue(), '{{file::')
|| 0 === strpos($config->getValue(), $this->uploadPath)
)
) {
return true;
}

if ('file' === $config->getContext() && Validator::isUuid($config->getValue())) {
return true;
if ('file' === $config->getContext()) {
return Validator::isUuid($config->getValue());
}

return false;
return false !== strpos($config->getValue(), '{{file::')
|| 0 === strpos($config->getValue(), $this->uploadPath)
;
}

/**
Expand All @@ -93,11 +87,10 @@ public function getDcaTable()
*/
public function getDcaAttributes(PickerConfig $config)
{
$attributes = [];
$value = $config->getValue();

if ('file' === $config->getContext()) {
$attributes += array_intersect_key(
$attributes = array_intersect_key(
$config->getExtras(),
array_flip(['fieldType', 'files', 'filesOnly', 'path', 'extensions'])
);
Expand All @@ -109,20 +102,24 @@ public function getDcaAttributes(PickerConfig $config)
$attributes['value'][] = $this->urlEncode($this->convertValueToPath($v));
}
}
} elseif ('link' === $config->getContext()) {
$attributes['fieldType'] = 'radio';
$attributes['filesOnly'] = true;

if ($value) {
if (false !== strpos($value, '{{file::')) {
$value = str_replace(['{{file::', '}}'], '', $value);
}
return $attributes;
}

if (0 === strpos($value, $this->uploadPath.'/')) {
$attributes['value'] = $this->urlEncode($value);
} else {
$attributes['value'] = $this->urlEncode($this->convertValueToPath($value));
}
$attributes = [
'fieldType' => 'radio',
'filesOnly' => true,
];

if ($value) {
if (false !== strpos($value, '{{file::')) {
$value = str_replace(['{{file::', '}}'], '', $value);
}

if (0 === strpos($value, $this->uploadPath.'/')) {
$attributes['value'] = $this->urlEncode($value);
} else {
$attributes['value'] = $this->urlEncode($this->convertValueToPath($value));
}
}

Expand All @@ -134,14 +131,13 @@ public function getDcaAttributes(PickerConfig $config)
*/
public function convertDcaValue(PickerConfig $config, $value)
{
if ('link' === $config->getContext()) {
/** @var FilesModel $filesModel */
$filesModel = $this->framework->getAdapter(FilesModel::class);
$file = $filesModel->findByPath(rawurldecode($value));
/** @var FilesModel $filesAdapter */
$filesAdapter = $this->framework->getAdapter(FilesModel::class);

if (null !== $file) {
return '{{file::'.StringUtil::binToUuid($file->uuid).'}}';
}
$filesModel = $filesAdapter->findByPath(rawurldecode($value));

if ($filesModel instanceof FilesModel) {
return '{{file::'.StringUtil::binToUuid($filesModel->uuid).'}}';
}

return $value;
Expand Down Expand Up @@ -172,11 +168,11 @@ protected function getRouteParameters(PickerConfig $config)
*/
private function convertValueToPath($value)
{
/** @var FilesModel $filesModel */
$filesModel = $this->framework->getAdapter(FilesModel::class);
/** @var FilesModel $filesAdapter */
$filesAdapter = $this->framework->getAdapter(FilesModel::class);

if (Validator::isUuid($value) && ($file = $filesModel->findByUuid($value)) instanceof FilesModel) {
return $file->path;
if (Validator::isUuid($value) && ($filesModel = $filesAdapter->findByUuid($value)) instanceof FilesModel) {
return $filesModel->path;
}

return $value;
Expand Down
29 changes: 13 additions & 16 deletions src/Picker/PagePickerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,11 @@ public function supportsContext($context)
*/
public function supportsValue(PickerConfig $config)
{
if ('link' === $config->getContext() && false !== strpos($config->getValue(), '{{link_url::')) {
return true;
}

if ('page' === $config->getContext() && is_numeric($config->getValue())) {
return true;
if ('page' === $config->getContext()) {
return is_numeric($config->getValue());
}

return false;
return false !== strpos($config->getValue(), '{{link_url::');
}

/**
Expand All @@ -62,7 +58,6 @@ public function getDcaTable()
*/
public function getDcaAttributes(PickerConfig $config)
{
$attributes = [];
$value = $config->getValue();

if ('page' === $config->getContext()) {
Expand All @@ -75,12 +70,14 @@ public function getDcaAttributes(PickerConfig $config)
if ($value) {
$attributes['value'] = array_map('intval', explode(',', $value));
}
} elseif ('link' === $config->getContext()) {
$attributes = ['fieldType' => 'radio'];

if ($value && false !== strpos($value, '{{link_url::')) {
$attributes['value'] = str_replace(['{{link_url::', '}}'], '', $value);
}
return $attributes;
}

$attributes = ['fieldType' => 'radio'];

if ($value && false !== strpos($value, '{{link_url::')) {
$attributes['value'] = str_replace(['{{link_url::', '}}'], '', $value);
}

return $attributes;
Expand All @@ -91,11 +88,11 @@ public function getDcaAttributes(PickerConfig $config)
*/
public function convertDcaValue(PickerConfig $config, $value)
{
if ('link' === $config->getContext()) {
return '{{link_url::'.$value.'}}';
if ('page' === $config->getContext()) {
return (int) $value;
}

return (int) $value;
return '{{link_url::'.$value.'}}';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Picker/Picker.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function getCurrentUrl()
throw new \RuntimeException('No picker menu items found.');
}

/** @var ItemInterface $menu */
/** @var ItemInterface[] $menu */
foreach ($menu as $item) {
$picker = $item->getExtra('provider');

Expand Down
28 changes: 13 additions & 15 deletions src/Picker/PickerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public function getContext()
return $this->context;
}

/**
* Returns the extras.
*
* @return array
*/
public function getExtras()
{
return $this->extras;
}

/**
* Returns the value.
*
Expand All @@ -83,16 +93,6 @@ public function getCurrent()
return $this->current;
}

/**
* Returns the extras.
*
* @return array
*/
public function getExtras()
{
return $this->extras;
}

/**
* Returns an extra by name.
*
Expand Down Expand Up @@ -135,24 +135,22 @@ public function jsonSerialize()
{
return [
'context' => $this->context,
'extras' => $this->extras,
'current' => $this->current,
'value' => $this->value,
'extras' => $this->extras,
];
}

/**
* Encodes the picker configuration for the URL.
*
* @param bool $compress
*
* @return string
*/
public function urlEncode($compress = true)
public function urlEncode()
{
$data = json_encode($this);

if ($compress && function_exists('gzencode') && false !== ($encoded = @gzencode($data))) {
if (function_exists('gzencode') && false !== ($encoded = @gzencode($data))) {
$data = $encoded;
}

Expand Down
Loading

0 comments on commit 0d83931

Please sign in to comment.