Skip to content

Commit

Permalink
Merge pull request #405 from FriendsOfCake/chore/cleanup
Browse files Browse the repository at this point in the history
Remove unneeded code and ARIA attributes.
  • Loading branch information
ADmad authored Nov 18, 2024
2 parents c73af02 + 03a8dbb commit 129c4ee
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 209 deletions.
2 changes: 1 addition & 1 deletion .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpstan" version="2.0.1" installed="2.0.1" location="./tools/phpstan" copy="false"/>
<phar name="phpstan" version="2.0.2" installed="2.0.2" location="./tools/phpstan" copy="false"/>
<phar name="psalm" version="5.26.1" installed="5.26.1" location="./tools/psalm" copy="false"/>
</phive>
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
}
],
"require": {
"cakephp/cakephp": "^5.0"
"cakephp/cakephp": "^5.1"
},
"require-dev": {
"phpunit/phpunit": "^10.1",
"phpunit/phpunit": "^10.5.5 || ^11.1.3",
"cakephp/bake": "^3.0",
"cakephp/cakephp-codesniffer": "^5.0"
"cakephp/cakephp-codesniffer": "^5.1"
},
"support": {
"issues": "http://github.com/friendsofcake/bootstrap-ui/issues",
Expand Down
126 changes: 3 additions & 123 deletions src/View/Helper/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,6 @@ class FormHelper extends CoreFormHelper
'<span data-bs-toggle="tooltip" title="{{content}}" class="bi bi-info-circle-fill"></span>',
'formGroupFloatingLabel' =>
'{{input}}{{label}}',
'datetimeContainer' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{type}}{{required}}">{{content}}{{help}}</div>',
'datetimeContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{formGroupPosition}}{{type}}{{required}} is-invalid">' .
'{{content}}{{error}}{{help}}</div>',
'datetimeLabel' =>
'<label{{attrs}}>{{text}}{{tooltip}}</label>',
'inputContainer' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{type}}{{required}}">{{content}}{{help}}</div>',
Expand Down Expand Up @@ -242,16 +233,6 @@ class FormHelper extends CoreFormHelper
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-check{{variant}} ' .
'{{formGroupPosition}}{{type}}{{required}} is-invalid">{{content}}{{error}}{{help}}</div>',
'datetimeContainer' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{formGroupPosition}}{{type}}{{required}}">' .
'{{content}}{{help}}</div>',
'datetimeContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{formGroupPosition}}{{type}}{{required}} is-invalid">' .
'{{content}}{{error}}{{help}}</div>',
'datetimeLabel' =>
'<label{{attrs}}>{{text}}{{tooltip}}</label>',
'radioContainer' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{formGroupPosition}}{{type}}{{required}}" role="group" ' .
Expand Down Expand Up @@ -287,15 +268,6 @@ class FormHelper extends CoreFormHelper
'<div class="%s form-floating">{{input}}{{label}}{{error}}{{help}}</div>',
'checkboxFormGroup' =>
'<div class="%s"><div class="form-check{{variant}}">{{input}}{{label}}{{error}}{{help}}</div></div>',
'datetimeContainer' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group row {{type}}{{required}}">{{content}}</div>',
'datetimeContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group row {{formGroupPosition}}{{type}}{{required}} is-invalid">' .
'{{content}}</div>',
'datetimeLabel' =>
'<label{{attrs}}>{{text}}{{tooltip}}</label>',
'checkboxInlineFormGroup' =>
'<div class="%s"><div class="form-check{{variant}} form-check-inline">{{input}}{{label}}</div></div>',
'submitContainer' =>
Expand Down Expand Up @@ -350,13 +322,6 @@ class FormHelper extends CoreFormHelper
'_default' => 'BootstrapUI\View\Widget\BasicWidget',
];

/**
* The name of the field for which the current error is being generated.
*
* @var string|null
*/
private ?string $_errorFieldName = null;

/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -414,40 +379,6 @@ public function create(mixed $context = null, array $options = []): string
return parent::create($context, $this->_processFormOptions($options));
}

/**
* @inheritDoc
*/
public function error(string $field, array|string|null $text = null, array $options = []): string
{
$this->_errorFieldName = $field;
$error = parent::error($field, $text, $options);
$this->_errorFieldName = null;

return $error;
}

/**
* @inheritDoc
*/
public function formatTemplate(string $name, array $data): string
{
// Injects the `id` attribute value for the error template.
// This is done for backwards compatibility reasons, as the
// core form helper only introduced this behavior with
// CakePHP 4.3. This can be removed once the required minimum
// CakePHP version is bumped accordingly.

if (
$name === 'error' &&
!isset($data['id']) &&
$this->_errorFieldName !== null
) {
$data['id'] = $this->_domId($this->_errorFieldName . '-error');
}

return parent::formatTemplate($name, $data);
}

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -548,13 +479,6 @@ public function control(string $fieldName, array $options = []): string
}

switch ($options['type']) {
case 'datetime-local':
case 'datetime':
case 'date':
case 'time':
$options = $this->_dateTimeOptions($fieldName, $options);
break;

case 'checkbox':
case 'radio':
case 'select':
Expand Down Expand Up @@ -676,35 +600,6 @@ protected function _containerOptions(?string $fieldName, array $options): array
return $options;
}

/**
* Modify options for date time controls.
*
* @param string $fieldName Field name.
* @param array $options Options. See `$options` argument of `control()` method.
* @return array
*/
protected function _dateTimeOptions(string $fieldName, array $options): array
{
$options = $this->_labelOptions($fieldName, $options);

// group IDs are no longer required for date/time controls,
// this is just kept for backwards compatibility

$groupId =
$options['templateVars']['groupId'] =
$this->_domId($fieldName . '-group-label');

if ($options['label'] !== false) {
$options['label']['templateVars']['groupId'] = $groupId;
}

$options['templates']['label'] = $this->templater()->get('datetimeLabel');
$options['templates']['inputContainer'] = $this->templater()->get('datetimeContainer');
$options['templates']['inputContainerError'] = $this->templater()->get('datetimeContainerError');

return $options;
}

/**
* Modify options for checkbox controls.
*
Expand Down Expand Up @@ -985,8 +880,8 @@ protected function _ariaOptions(string $fieldName, array $options): array
{
if (
$options['type'] === 'hidden' ||
($options['type'] === 'select' && isset($options['multiple']) && $options['multiple'] === 'checkbox') ||
(
isset($options['aria-required']) &&
isset($options['aria-describedby']) &&
isset($options['aria-invalid'])
)
Expand All @@ -998,26 +893,15 @@ protected function _ariaOptions(string $fieldName, array $options): array
$options['error'] !== false &&
$this->isFieldError($fieldName);

// `aria-invalid` and `aria-required` are injected for backwards
// compatibility reasons, as support for this has only been
// introduced in the core form helper with CakePHP 4.3. This can
// be removed once the required minimum CakePHP version is bumped
// accordingly.

// This is required because of a bug in CakePHP 5 due to which
// `aria-invalid` is not applied for `select` tags.
if (
$isError &&
!isset($options['aria-invalid'])
) {
$options['aria-invalid'] = 'true';
}

if (
$options['required'] &&
!isset($options['aria-required'])
) {
$options['aria-required'] = 'true';
}

if (isset($options['aria-describedby'])) {
return $options;
}
Expand Down Expand Up @@ -1435,10 +1319,6 @@ protected function _processFormOptions(array $options): array
$templates['label'],
$this->_gridClass(static::GRID_COLUMN_ONE)
);
$templates['datetimeLabel'] = sprintf(
$templates['datetimeLabel'],
$this->_gridClass(static::GRID_COLUMN_ONE)
);
$templates['radioLabel'] = sprintf(
$templates['radioLabel'],
$this->_gridClass(static::GRID_COLUMN_ONE)
Expand Down
6 changes: 0 additions & 6 deletions tests/TestCase/Bake/BakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ public function setUp(): void
parent::setUp();

$this->_compareBasePath = Plugin::path('BootstrapUI') . 'tests' . DS . 'comparisons' . DS . 'Template' . DS;

$this->loadPlugins([
'Cake/TwigView' => [
'bootstrap' => true,
],
]);
}

public function tearDown(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Command/InstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,12 @@ public function testNPMInstallFailure()
$command
->expects($this->once())
->method('_runNPMInstall')
->will($this->returnCallback(function (&$out, &$return) {
->willReturnCallback(function (&$out, &$return) {
$out = [
'installer output',
];
$return = 1234;
}));
});

$out = new StubConsoleOutput();
$err = new StubConsoleOutput();
Expand Down
12 changes: 7 additions & 5 deletions tests/TestCase/View/Helper/FlashHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Cake\Http\Session;
use Cake\TestSuite\TestCase;
use Cake\View\View;
use PHPUnit\Framework\Attributes\DataProvider;
use UnexpectedValueException;

/**
Expand Down Expand Up @@ -63,11 +64,11 @@ public static function flashTypeDefaultsDataProvider(): array
}

/**
* @dataProvider flashTypeDefaultsDataProvider
* @param string $type The flash type.
* @param string $class The alert class.
* @param string $icon The icon name.
*/
#[DataProvider('flashTypeDefaultsDataProvider')]
public function testRenderDefaults(string $type, string $class, string $icon)
{
$this->View->getRequest()->getSession()->write('Flash', [
Expand Down Expand Up @@ -445,9 +446,9 @@ public static function flashTypeDataProvider(): array
}

/**
* @dataProvider flashTypeDataProvider
* @param string $type The flash type.
*/
#[DataProvider('flashTypeDataProvider')]
public function testCustomDefaultIcon(string $type)
{
$this->Flash->setConfig('icon', 'info');
Expand All @@ -466,9 +467,9 @@ public function testCustomDefaultIcon(string $type)
}

/**
* @dataProvider flashTypeDataProvider
* @param string $type The flash type.
*/
#[DataProvider('flashTypeDataProvider')]
public function testDisableIconsViaConfig(string $type)
{
$this->Flash->setConfig('icon', false);
Expand All @@ -490,6 +491,7 @@ public function testDisableIconsViaConfig(string $type)
* @dataProvider flashTypeDataProvider
* @param string $type The flash type.
*/
#[DataProvider('flashTypeDataProvider')]
public function testDisableIconViaParamsOption(string $type)
{
$this->View->getRequest()->getSession()->write('Flash', [
Expand All @@ -508,9 +510,9 @@ public function testDisableIconViaParamsOption(string $type)
}

/**
* @dataProvider flashTypeDataProvider
* @param string $type The flash type.
*/
#[DataProvider('flashTypeDataProvider')]
public function testCustomIconClassMap(string $type)
{
$iconClassMap = [
Expand Down Expand Up @@ -540,9 +542,9 @@ public function testCustomIconClassMap(string $type)
}

/**
* @dataProvider flashTypeDataProvider
* @param string $type The flash type.
*/
#[DataProvider('flashTypeDataProvider')]
public function testCustomIconClassMapOptions(string $type)
{
$options = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,24 +559,24 @@ public function testDefaultAlignDateTimeControlCustomContainerTemplateViaTemplat
$this->assertStringContainsString('<div class="mb-3 form-group time">', $result);

$this->Form->setTemplates([
'datetimeContainer' => '<div class="custom datetimeContainer {{type}}{{required}}" role="group" aria-labelledby="{{groupId}}">{{content}}</div>',
'dateContainer' => '<div class="custom dateContainer {{type}}{{required}}" role="group" aria-labelledby="{{groupId}}">{{content}}</div>',
'timeContainer' => '<div class="custom timeContainer {{type}}{{required}}" role="group" aria-labelledby="{{groupId}}">{{content}}</div>',
'datetimeContainer' => '<div class="custom datetimeContainer {{type}}{{required}}">{{content}}</div>',
'dateContainer' => '<div class="custom dateContainer {{type}}{{required}}">{{content}}</div>',
'timeContainer' => '<div class="custom timeContainer {{type}}{{required}}">{{content}}</div>',
]);
$result = $this->Form->control('created', [
'type' => 'datetime',
]);
$this->assertStringContainsString('<div class="custom datetimeContainer datetime" role="group" aria-labelledby="created-group-label">', $result);
$this->assertStringContainsString('<div class="custom datetimeContainer datetime">', $result);

$result = $this->Form->control('created', [
'type' => 'date',
]);
$this->assertStringContainsString('<div class="custom dateContainer date" role="group" aria-labelledby="created-group-label">', $result);
$this->assertStringContainsString('<div class="custom dateContainer date">', $result);

$result = $this->Form->control('created', [
'type' => 'time',
]);
$this->assertStringContainsString('<div class="custom timeContainer time" role="group" aria-labelledby="created-group-label">', $result);
$this->assertStringContainsString('<div class="custom timeContainer time">', $result);
}

public function testDefaultAlignDateTimeControlCustomContainerErrorTemplateViaOptions()
Expand Down Expand Up @@ -606,25 +606,25 @@ public function testDefaultAlignDateTimeControlCustomContainerErrorTemplateViaOp
$result = $this->Form->control('created', [
'type' => 'datetime',
'templates' => [
'datetimeContainerError' => '<div class="custom datetimeContainerError {{type}}{{required}}" role="group" aria-labelledby="{{groupId}}">{{content}}</div>',
'datetimeContainerError' => '<div class="custom datetimeContainerError {{type}}{{required}}">{{content}}</div>',
],
]);
$this->assertStringContainsString('<div class="custom datetimeContainerError datetime" role="group" aria-labelledby="created-group-label">', $result);
$this->assertStringContainsString('<div class="custom datetimeContainerError datetime">', $result);

$result = $this->Form->control('created', [
'type' => 'date',
'templates' => [
'dateContainerError' => '<div class="custom dateContainerError {{type}}{{required}}" role="group" aria-labelledby="{{groupId}}">{{content}}</div>',
'dateContainerError' => '<div class="custom dateContainerError {{type}}{{required}}">{{content}}</div>',
],
]);
$this->assertStringContainsString('<div class="custom dateContainerError date" role="group" aria-labelledby="created-group-label">', $result);
$this->assertStringContainsString('<div class="custom dateContainerError date">', $result);

$result = $this->Form->control('created', [
'type' => 'time',
'templates' => [
'timeContainerError' => '<div class="custom timeContainerError {{type}}{{required}}" role="group" aria-labelledby="{{groupId}}">{{content}}</div>',
'timeContainerError' => '<div class="custom timeContainerError {{type}}{{required}}">{{content}}</div>',
],
]);
$this->assertStringContainsString('<div class="custom timeContainerError time" role="group" aria-labelledby="created-group-label">', $result);
$this->assertStringContainsString('<div class="custom timeContainerError time">', $result);
}
}
Loading

0 comments on commit 129c4ee

Please sign in to comment.