Skip to content

Commit

Permalink
fixed strict comparison issues, enhanced translation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Patzer committed Dec 6, 2018
1 parent c323c93 commit 70384a2
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 8 deletions.
1 change: 1 addition & 0 deletions .php_cs
Expand Up @@ -38,6 +38,7 @@ return PhpCsFixer\Config::create()
'expectedException',
'expectedExceptionMessage',
],
'blank_line_before_statement' => ['statements' => ['break', 'case', 'continue', 'declare', 'default', 'die', 'do', 'exit', 'for', 'foreach', 'goto', 'if', 'include', 'include_once', 'require', 'require_once', 'return', 'switch', 'throw', 'try', 'while', 'yield']]
])
->setFinder($finder)
->setRiskyAllowed(true)
Expand Down
15 changes: 10 additions & 5 deletions src/Backend/ReaderConfigElement.php
Expand Up @@ -93,15 +93,16 @@ public function checkPermission()
break;

case 'create':
if (!\strlen(Input::get('pid')) || !\in_array(Input::get('pid'), $root, true)) {
if (!\strlen(Input::get('pid')) || !\in_array(Input::get('pid'), $root)) {
throw new AccessDeniedException('Not enough permissions to create reader_config_element items in reader_config_element archive ID '
.Input::get('pid').'.');
}

break;

case 'cut':
case 'copy':
if (!\in_array(Input::get('pid'), $root, true)) {
if (!\in_array(Input::get('pid'), $root)) {
throw new AccessDeniedException('Not enough permissions to '.Input::get('act').' reader_config_element item ID '.$id
.' to reader_config_element archive ID '.Input::get('pid').'.');
}
Expand All @@ -118,10 +119,11 @@ public function checkPermission()
throw new AccessDeniedException('Invalid reader_config_element item ID '.$id.'.');
}

if (!\in_array($objArchive->pid, $root, true)) {
if (!\in_array($objArchive->pid, $root)) {
throw new AccessDeniedException('Not enough permissions to '.Input::get('act').' reader_config_element item ID '.$id
.' of reader_config_element archive ID '.$objArchive->pid.'.');
}

break;

case 'select':
Expand All @@ -130,7 +132,7 @@ public function checkPermission()
case 'overrideAll':
case 'cutAll':
case 'copyAll':
if (!\in_array($id, $root, true)) {
if (!\in_array($id, $root)) {
throw new AccessDeniedException('Not enough permissions to access reader_config_element archive ID '.$id.'.');
}

Expand All @@ -146,14 +148,16 @@ public function checkPermission()
$session = $session->all();
$session['CURRENT']['IDS'] = array_intersect($session['CURRENT']['IDS'], $objArchive->fetchEach('id'));
$session->replace($session);

break;

default:
if (\strlen(Input::get('act'))) {
throw new AccessDeniedException('Invalid command "'.Input::get('act').'".');
} elseif (!\in_array($id, $root, true)) {
} elseif (!\in_array($id, $root)) {
throw new AccessDeniedException('Not enough permissions to access reader_config_element archive ID '.$id.'.');
}

break;
}
}
Expand Down Expand Up @@ -196,6 +200,7 @@ public function getFieldsAsOptions(DataContainer $dc)

return array_keys($fields);
}

throw new \Exception("No 'table' set in $dc->table.$dc->field's eval array.");
}

Expand Down
1 change: 1 addition & 0 deletions src/ConfigElementType/CommentConfigElementType.php
Expand Up @@ -120,6 +120,7 @@ protected function modifyFields(array $standardFields, ReaderConfigElementModel
}

$fields = [];

foreach ($standardFields as $key => $field) {
if (!\in_array($key, $overridePalette)) {
continue;
Expand Down
1 change: 1 addition & 0 deletions src/ConfigElementType/Delete/DefaultDelete.php
Expand Up @@ -77,6 +77,7 @@ protected function redirectAfterDelete($jumpTo)
{
/** @var PageModel $pageModel */
$pageModel = $this->framework->getAdapter(PageModel::class)->findPublishedById($jumpTo);

if (null === $pageModel) {
throw new PageNotFoundException('The redirect page with the id:'.$jumpTo.'does not exist.');
}
Expand Down
4 changes: 4 additions & 0 deletions src/ConfigElementType/DeleteConfigElementType.php
Expand Up @@ -40,22 +40,26 @@ public function addToItemData(ItemInterface $item, ReaderConfigElementModel $rea
$redirectParams = StringUtil::deserialize($readerConfigElement->redirectParams, true);
$deleteConditions = false;
$request = System::getContainer()->get('huh.request');

foreach ($redirectParams as $redirectParam) {
if (ReaderConfigElement::REDIRECTION_PARAM_TYPE_DEFAULT_VALUE === $redirectParam['parameterType'] && (!$request->hasGet($redirectParam['name']) || $redirectParam['defaultValue'] !== $request->getGet($redirectParam['name']))) {
$deleteConditions = false;

break;
}
$deleteConditions = true;

if (ReaderConfigElement::REDIRECTION_PARAM_TYPE_FIELD_VALUE === $redirectParam['parameterType'] && !$request->hasGet($redirectParam['name'])) {
$deleteConditions = false;

break;
}
$deleteConditions = true;
}

if ($deleteConditions) {
$class = $this->getDeleteClassByName($readerConfigElement->deleteClass);

if (null === $class) {
return;
}
Expand Down
3 changes: 3 additions & 0 deletions src/ConfigElementType/ImageConfigElementType.php
Expand Up @@ -52,9 +52,12 @@ public function addToItemData(ItemInterface $item, ReaderConfigElementModel $rea
} else {
$image = $readerConfigElement->placeholderImage;
}

break;

case ReaderConfigElement::PLACEHOLDER_IMAGE_MODE_SIMPLE:
$image = $readerConfigElement->placeholderImage;

break;
}
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/ConfigElementType/RedirectionConfigElementType.php
Expand Up @@ -38,6 +38,7 @@ public function addToItemData(ItemInterface $item, ReaderConfigElementModel $rea
}
/** @var PageModel $pageModel */
$pageModel = $this->framework->getAdapter(PageModel::class)->findPublishedById($readerConfigElement->jumpTo);

if (null === $pageModel) {
return;
}
Expand All @@ -50,6 +51,7 @@ public function addToItemData(ItemInterface $item, ReaderConfigElementModel $rea

if ($readerConfigElement->addRedirectParam) {
$redirectParams = StringUtil::deserialize($readerConfigElement->redirectParams, true);

foreach ($redirectParams as $redirectParam) {
if (ReaderConfigElement::REDIRECTION_PARAM_TYPE_FIELD_VALUE === $redirectParam['parameterType']) {
$param = $redirectParam['field'];
Expand Down
1 change: 1 addition & 0 deletions src/ConfigElementType/Syndication/IcsSyndication.php
Expand Up @@ -128,6 +128,7 @@ public function exportIcs(ItemInterface $item)

$response = new Response($calendarExport->getStream());
$response->headers->set('Content-Type', 'text/calendar');

throw new ResponseException($response);
}

Expand Down
6 changes: 6 additions & 0 deletions src/EventListener/ReaderConfigCallbackListener.php
Expand Up @@ -50,20 +50,24 @@ public function updateLabel(DC_Table $dc)
{
/** @var Input $input */
$input = $this->framework->getAdapter(Input::class);

if (!$input->get('act') || 'edit' !== $input->get('act')) {
return;
}

if (!$this->translator instanceof TranslatorBagInterface) {
return;
}
$table = $dc->table;
$configModel = $this->modelUtil->findModelInstanceByIdOrAlias($table, $dc->id);

if (!$configModel) {
return;
}
$type = $configModel->type;
Controller::loadDataContainer($table);
$dca = &$GLOBALS['TL_DCA'][$table];

if (!strpos($dca['palettes'][$type], static::SELECTOR_FIELD)) {
return;
}
Expand All @@ -75,9 +79,11 @@ public function updateLabel(DC_Table $dc)
$this->translator->trans('huh.reader.tl_reader_config_element.field.typeSelectorField.'.$type.'.desc'),
];
}

if (!strpos($dca['palettes'][$type], static::TYPE_FIELD)) {
return;
}

if ($this->translator->getCatalogue()->has("huh.reader.tl_reader_config_element.field.typeField.$type.name") &&
$this->translator->getCatalogue()->has("huh.reader.tl_reader_config_element.field.typeField.$type.desc")) {
$dca['fields'][static::TYPE_FIELD]['label'] = [
Expand Down
2 changes: 1 addition & 1 deletion src/Item/DefaultItem.php
Expand Up @@ -166,7 +166,7 @@ public function setFormattedValue(string $name, $value): void
true
) : (isset($dca['fields']) && \is_array($dca['fields']) ? array_keys($dca['fields']) : []);

if (\in_array($name, $fields, true)) {
if (\in_array($name, $fields)) {
$this->dc->field = $name;

$value = $this->_manager->getFormUtil()->prepareSpecialValueForOutput(
Expand Down
8 changes: 7 additions & 1 deletion src/Manager/ReaderManager.php
Expand Up @@ -163,9 +163,12 @@ public function retrieveItem(): ?ItemInterface
switch ($readerConfig->itemRetrievalMode) {
case ReaderConfig::ITEM_RETRIEVAL_MODE_AUTO_ITEM:
$item = $this->retrieveItemByAutoItem();

break;

case ReaderConfig::ITEM_RETRIEVAL_MODE_FIELD_CONDITIONS:
$item = $this->retrieveItemByFieldConditions();

break;
}

Expand All @@ -178,7 +181,7 @@ public function retrieveItem(): ?ItemInterface
$itemFields = array_keys($item);

foreach (array_keys($GLOBALS['TL_DCA'][$readerConfig->dataContainer]['fields']) as $field) {
if (!\in_array($field, $itemFields, true)) {
if (!\in_array($field, $itemFields)) {
$item[$field] = null;
}
}
Expand Down Expand Up @@ -398,7 +401,9 @@ function (array $matches) use ($item) {
case 'huh.head.tag.title':
global $objPage;
$objPage->pageTitle = $value;

break;

default:
System::getContainer()->get($service)->setContent($value);
}
Expand Down Expand Up @@ -602,6 +607,7 @@ protected function retrieveItemByAutoItem()

/* @var Model $adapter */
$adapter = $this->framework->getAdapter(Model::class);

if (!($modelClass = $adapter->getClassFromTable($readerConfig->dataContainer))) {
return $item;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Registry/ReaderConfigRegistry.php
Expand Up @@ -206,7 +206,7 @@ public function getReaderManagerByName(string $name): ?ReaderManagerInterface
$manager = System::getContainer()->get($manager['id']);
$interfaces = class_implements($manager);

if (!\is_array($interfaces) || !\in_array(ReaderManagerInterface::class, $interfaces, true)) {
if (!\is_array($interfaces) || !\in_array(ReaderManagerInterface::class, $interfaces)) {
throw new \Exception(sprintf('Reader manager service %s must implement %s', $manager['id'], ReaderManagerInterface::class));
}

Expand Down
21 changes: 21 additions & 0 deletions tests/Manager/ReaderManagerTest.php
Expand Up @@ -260,12 +260,17 @@ function ($field, $value, $dc) {
switch ($field) {
case 'firstname':
return $value;

break;

case 'lastname':
return $value;

break;

case 'someDate':
return '02.03.2018';

break;
}
}
Expand All @@ -276,12 +281,17 @@ function ($table, $field, $value) {
switch ($field) {
case 'firstname':
return $value;

break;

case 'lastname':
return $value;

break;

case 'someDate':
return '02.03.2018';

break;
}
}
Expand Down Expand Up @@ -377,22 +387,27 @@ function ($uuid) {
'path' => 'data/image.png',
]
);

break;

case 'female':
return $this->mockClassWithProperties(
FilesModel::class,
[
'path' => 'data/female.png',
]
);

break;

case 'male':
return $this->mockClassWithProperties(
FilesModel::class,
[
'path' => 'data/male.png',
]
);

break;
}
}
Expand All @@ -409,10 +424,14 @@ function ($class) use ($databaseAdapter) {
switch ($class) {
case Database::class:
return $databaseAdapter;

break;

case ImageConfigElementType::class:
return new ImageConfigElementType($this->framework);

break;

default:
return null;
}
Expand Down Expand Up @@ -653,7 +672,9 @@ function ($id) use ($readerConfigMock) {
switch ($id) {
case 1:
return $readerConfigMock;

break;

default:
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCaseEnvironment.php
Expand Up @@ -65,8 +65,10 @@ public function createRouterMock()
$router = $this->createMock(RouterInterface::class);
$router->method('generate')->with('contao_backend', $this->anything())->will($this->returnCallback(function ($route, $params = []) {
$url = '/contao';

if (!empty($params)) {
$count = 0;

foreach ($params as $key => $value) {
$url .= (0 === $count ? '?' : '&');
$url .= $key.'='.$value;
Expand Down

0 comments on commit 70384a2

Please sign in to comment.