Skip to content

Commit

Permalink
Prepare 12.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkla committed Nov 17, 2021
1 parent bc09045 commit 4eb2351
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 141 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 12.0.1

- Removed field group class

## 12.0.0

- Updated minimum PHP version to 8.0
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "12-dev"
"dev-master": "12.1-dev"
}
},
"autoload": {
Expand Down
46 changes: 0 additions & 46 deletions src/FieldGroup.php

This file was deleted.

28 changes: 23 additions & 5 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,32 @@

declare(strict_types=1);

use WordPlate\Acf\FieldGroup;
use WordPlate\Acf\Key;

if (!function_exists('register_extended_field_group')) {
/** Register ACF field group. */
function register_extended_field_group(array $config): void
/** @throws \InvalidArgumentException */
function register_extended_field_group(array $settings): array
{
$fieldGroup = new FieldGroup($config);
$requiredKeys = ['title', 'fields', 'location'];

register_field_group($fieldGroup->get());
foreach ($requiredKeys as $key) {
if (!array_key_exists($key, $settings)) {
throw new InvalidArgumentException("Missing field group setting [$key].");
}
}

$key = Key::sanitize($settings['key'] ?? $settings['title']);

$settings['style'] = $settings['style'] ?? 'seamless';

$settings['fields'] = array_map(fn ($field) => $field->get($key), $settings['fields']);

$settings['location'] = array_map(fn ($location) => $location->get(), $settings['location']);

$settings['key'] = Key::generate($key, 'group');

register_field_group($settings);

return $settings;
}
}
8 changes: 3 additions & 5 deletions tests/ConditionalLogicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testInvalidOperator()

public function testResolvedParentKey()
{
$fieldGroup = new FieldGroup([
$settings = register_extended_field_group([
'title' => 'Resolved Parent Key',
'fields' => [
Select::make('Select')
Expand All @@ -63,11 +63,9 @@ public function testResolvedParentKey()
'location' => []
]);

$config = $fieldGroup->get();

$this->assertSame(
$config['fields'][0]['key'],
$config['fields'][1]['sub_fields'][0]['conditional_logic'][0][0]['field']
$settings['fields'][0]['key'],
$settings['fields'][1]['sub_fields'][0]['conditional_logic'][0][0]['field']
);
}
}
79 changes: 0 additions & 79 deletions tests/FieldGroupTest.php

This file was deleted.

59 changes: 54 additions & 5 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,65 @@

namespace WordPlate\Tests\Acf;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use WordPlate\Acf\Fields\Text;
use WordPlate\Acf\Location;

class HelpersTest extends TestCase
{
public function testRegisterExtendedFieldGroup()
public function testFields()
{
$this->assertEmpty(register_extended_field_group([
'title' => 'Helper',
'fields' => [],
$settings = register_extended_field_group([
'title' => 'Event',
'fields' => [
Text::make('Event Title'),
],
'location' => [],
]));
]);

$this->assertArrayHasKey('key', $settings['fields'][0]);
}

public function testKey()
{
$settings = register_extended_field_group(['title' => 'Apartment', 'fields' => [], 'location' => []]);
$this->assertSame('group_b1c2ce91', $settings['key']);
}

public function testCustomKey()
{
$settings = register_extended_field_group(['key' => 'house', 'title' => 'Apartment', 'fields' => [], 'location' => []]);
$this->assertSame('group_0cfb455b', $settings['key']);
}

public function testLocation()
{
$settings = register_extended_field_group([
'title' => 'Plant',
'fields' => [],
'location' => [
Location::where('post_type', 'page'),
],
]);

$location = $settings['location'];
$this->assertArrayHasKey('param', $location[0][0]);
}

public function testStyle()
{
$settings = register_extended_field_group(['title' => 'Employee', 'fields' => [], 'location' => []]);
$this->assertSame('seamless', $settings['style']);

$settings = register_extended_field_group(['title' => 'Student', 'fields' => [], 'location' => [], 'style' => 'default']);
$this->assertSame('default', $settings['style']);
}

public function testRequiredKeys()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing field group setting [title].');
register_extended_field_group(['fields' => [], 'location' => []]);
}
}

0 comments on commit 4eb2351

Please sign in to comment.