Skip to content
Snippets Groups Projects
Commit 13bbebcc authored by Jennifer Tehan's avatar Jennifer Tehan Committed by Elliot Ward
Browse files

Issue #3308219: Autocomplete widget value callback returns invalid value in...

Issue #3308219: Autocomplete widget value callback returns invalid value in between first set of parentheses
parent 044f59a4
Branches
Tags
1 merge request!1Issue #3308219: Autocomplete widget value callback returns invalid value in between first set of parentheses
......@@ -99,8 +99,8 @@ class TypedResourceObjectAutocompleteWidget extends WidgetBase {
// If input is being submitted..
if ($input !== FALSE) {
// .. convert this to an ID to store.
$opening_bracket_index = strpos($input, '(');
$closing_bracket_index = strpos($input, ')');
$opening_bracket_index = strrpos($input, '(');
$closing_bracket_index = strrpos($input, ')');
if ($opening_bracket_index === FALSE) {
return '';
}
......
<?php
namespace Drupal\Tests\jsonapi_reference\Unit;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\jsonapi_reference\Plugin\Field\FieldWidget\TypedResourceObjectAutocompleteWidget;
use Drupal\Tests\UnitTestCase;
/**
* Tests UUID processing in the widget.
*
* @group jsonapi_reference
*/
class UuidTest extends UnitTestCase {
/**
* Widget to test.
*
* @var \Drupal\jsonapi_reference\Plugin\Field\FieldWidget\TypedResourceObjectAutocompleteWidget
*/
protected $widget;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$field_definition = $this->prophesize(FieldDefinitionInterface::class);
$this->widget = new TypedResourceObjectAutocompleteWidget('typed_resource_object_autocomplete', [], $field_definition->reveal(), [], []);
}
/**
* Tests the Uuid can be found from the widget's input.
*/
public function testUuidLocation() {
$form_state = $this->prophesize(FormStateInterface::class);
$form_state->isRebuilding()->willReturn(FALSE);
// Test a title with the UUID at the end.
$result = $this->widget->valueCallback([], 'Node Title (d76037b2-2ec0-11ed-a261-0242ac120002)', $form_state->reveal());
self::assertEquals('d76037b2-2ec0-11ed-a261-0242ac120002', $result);
// Test the UUID can still be found when part of the title is in
// parentheses.
$result = $this->widget->valueCallback([], 'Node (Title) (d76037b2-2ec0-11ed-a261-0242ac120002)', $form_state->reveal());
self::assertEquals('d76037b2-2ec0-11ed-a261-0242ac120002', $result);
// Test lots more parentheses.
$result = $this->widget->valueCallback([], 'Hi (thing) (thang) (d76037b2-2ec0-11ed-a261-0242ac120002)', $form_state->reveal());
self::assertEquals('d76037b2-2ec0-11ed-a261-0242ac120002', $result);
// Test that the last UUID is the one returned if the title happens to be a
// UUID.
$result = $this->widget->valueCallback([], '(591ef6e4-2ec1-11ed-a261-0242ac120002) (d76037b2-2ec0-11ed-a261-0242ac120002)', $form_state->reveal());
self::assertEquals('d76037b2-2ec0-11ed-a261-0242ac120002', $result);
// Test what happens if there is only a UUID.
$result = $this->widget->valueCallback([], '(d76037b2-2ec0-11ed-a261-0242ac120002)', $form_state->reveal());
self::assertEquals('d76037b2-2ec0-11ed-a261-0242ac120002', $result);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment