Skip to content

Commit

Permalink
Merge branch '5.2' into 5
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jun 17, 2024
2 parents 049e546 + 1e0b905 commit b7e2a6e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Forms/SearchableDropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class SearchableDropdownField extends DropdownField implements HasOneRelationFieldInterface
{
use SearchableDropdownTrait;

// This needs to be defined on the class, not the trait, otherwise there is a PHP error
protected $schemaComponent = 'SearchableDropdownField';

Expand Down
28 changes: 25 additions & 3 deletions src/Forms/SearchableDropdownTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ public function getSource(): array
return $this->getListMap($this->sourceList);
}

public function Field($properties = [])
{
$context = $this;
$this->extend('onBeforeRender', $context, $properties);
return $context->customise($properties)->renderWith($context->getTemplates());
}

/*
* @param mixed $source
*/
Expand Down Expand Up @@ -450,9 +457,16 @@ public function getSchemaDataDefaults(): array

public function getSchemaStateDefaults(): array
{
$data = parent::getSchemaStateDefaults();
$data = $this->updateDataForSchema($data);
return $data;
$state = [
'name' => $this->getName(),
'id' => $this->ID(),
'value' => $this->getDefaultSchemaValue(),
'message' => $this->getSchemaMessage(),
'data' => [],
];

$state = $this->updateDataForSchema($state);
return $state;
}

/**
Expand All @@ -467,6 +481,14 @@ protected function setIsMultiple(bool $isMultiple): static
return $this;
}

private function getDefaultSchemaValue()
{
if (!$this->getIsLazyLoaded() && $this->hasMethod('getDefaultValue')) {
return $this->getDefaultValue();
}
return $this->Value();
}

private function getOptionsForSearchRequest(string $term): array
{
if (!$this->sourceList) {
Expand Down
44 changes: 44 additions & 0 deletions tests/php/Forms/SearchableDropdownTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\Forms\Tests;

use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\SearchableDropdownField;
Expand Down Expand Up @@ -217,4 +218,47 @@ public function testGetSchemaDataDefaults(): void
$this->assertSame('My placeholder', $schema['placeholder']);
$this->assertFalse($schema['searchable']);
}

public function provideLazyLoadedDoesntCallGetSource()
{
$methodsToCall = [
'Field',
'getSchemaStateDefaults',
'getSchemaState',
'getSchemaDataDefaults',
'getSchemaData',
];
$classes = [
SearchableMultiDropdownField::class,
SearchableDropdownField::class,
];
$scenarios = [];
foreach ($classes as $class) {
foreach ($methodsToCall as $method) {
$scenarios[] = [
'fieldClass' => $class,
'methodToCall' => $method,
];
}
}
return $scenarios;
}

/**
* @dataProvider provideLazyLoadedDoesntCallGetSource
*/
public function testLazyLoadedDoesntCallGetSource(string $fieldClass, string $methodToCall)
{
// Some methods aren't shared between the two form fields.
if (!ClassInfo::hasMethod($fieldClass, $methodToCall)) {
$this->markTestSkipped("$fieldClass doesn't have method $methodToCall - skipping");
}
// We have to disable the constructor because it ends up calling a static method, and we can't call static methods on mocks.
$mockField = $this->getMockBuilder($fieldClass)->onlyMethods(['getSource'])->disableOriginalConstructor()->getMock();
$mockField->expects($this->never())->method('getSource');
$mockField->setIsLazyLoaded(true);
$mockField->setSource(Team::get());
$mockField->setForm(new Form());
$mockField->$methodToCall();
}
}

0 comments on commit b7e2a6e

Please sign in to comment.