View source
<?php
namespace Drupal\Tests\conditional_fields\Unit;
use Drupal\conditional_fields\ConditionalFieldsFormHelper;
use Drupal\conditional_fields\ConditionalFieldsHandlersManager;
use Drupal\Core\Form\FormState;
use Drupal\Core\Render\ElementInfoManager;
use Drupal\node\NodeForm;
use Drupal\Tests\UnitTestCase;
class ConditionalFieldsFormHelperTest extends UnitTestCase {
public function testAddJavascriptEffects0Effects() {
$elementInfo = $this
->createMock(ElementInfoManager::class);
$cfHandlersManager = $this
->createMock(ConditionalFieldsHandlersManager::class);
$sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager);
$sutClass->effects = [];
$sutClass->form = [];
$sutClass
->addJavascriptEffects();
$this
->assertSame([
'#attached' => [
'library' => [
0 => 'conditional_fields/conditional_fields',
],
],
], $sutClass->form);
}
public function testAddJavascriptEffectsSomeEffects() {
$elementInfo = $this
->createMock(ElementInfoManager::class);
$cfHandlersManager = $this
->createMock(ConditionalFieldsHandlersManager::class);
$sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager);
$sutClass->effects = [
'some_effect',
];
$sutClass->form = [];
$sutClass
->addJavascriptEffects();
$this
->assertSame([
'#attached' => [
'library' => [
0 => 'conditional_fields/conditional_fields',
],
'drupalSettings' => [
'conditionalFields' => [
'effects' => [
'some_effect',
],
],
],
],
], $sutClass->form);
}
public function testAddValidationCallback() {
$elementInfo = $this
->createMock(ElementInfoManager::class);
$cfHandlersManager = $this
->createMock(ConditionalFieldsHandlersManager::class);
$sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager);
$formState = $this
->createMock(FormState::class);
$formState
->expects($this
->exactly(1))
->method('setValue')
->with('conditional_fields_untriggered_dependents', []);
$sutClass->form_state = $formState;
$sutClass->form = [];
$sutClass
->addValidationCallback();
$this
->assertSame([
'#validate' => [
0 => [
ConditionalFieldsFormHelper::class,
'formValidate',
],
],
], $sutClass->form);
}
public function testAfterBuild0Fields() {
$form = [];
$formState = $this
->createMock(FormState::class);
$sutClassNoCFields = $this
->createPartialMock(ConditionalFieldsFormHelper::class, [
'hasConditionalFields',
'processDependentFields',
'addJavascriptEffects',
'addValidationCallback',
]);
$sutClassNoCFields
->expects($this
->exactly(1))
->method('hasConditionalFields')
->willReturnOnConsecutiveCalls(FALSE);
$sutClassNoCFields
->expects($this
->exactly(0))
->method('processDependentFields');
$sutClassNoCFields
->expects($this
->exactly(0))
->method('addJavascriptEffects');
$sutClassNoCFields
->expects($this
->exactly(0))
->method('addValidationCallback');
$sutClassNoCFields
->afterBuild($form, $formState);
}
public function testAfterBuildSomeFields() {
$form = [];
$formState = $this
->createMock(FormState::class);
$sutClassSomeCFields = $this
->createPartialMock(ConditionalFieldsFormHelper::class, [
'hasConditionalFields',
'processDependentFields',
'addJavascriptEffects',
'addValidationCallback',
]);
$sutClassSomeCFields
->expects($this
->exactly(1))
->method('hasConditionalFields')
->willReturnOnConsecutiveCalls(TRUE);
$sutClassSomeCFields
->expects($this
->exactly(1))
->method('processDependentFields')
->willReturnOnConsecutiveCalls($sutClassSomeCFields);
$sutClassSomeCFields
->expects($this
->exactly(1))
->method('addJavascriptEffects')
->willReturnOnConsecutiveCalls($sutClassSomeCFields);
$sutClassSomeCFields
->expects($this
->exactly(1))
->method('addValidationCallback')
->willReturnOnConsecutiveCalls($sutClassSomeCFields);
$sutClassSomeCFields
->afterBuild($form, $formState);
}
public function testHasConditionalFields0Fields() {
$elementInfo = $this
->createMock(ElementInfoManager::class);
$cfHandlersManager = $this
->createMock(ConditionalFieldsHandlersManager::class);
$sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager);
$sutClass->form = [
'#conditional_fields' => [],
];
$formState = $this
->createMock(FormState::class);
$formState
->expects($this
->exactly(0))
->method('getFormObject');
$sutClass->form_state = $formState;
$result = $sutClass
->hasConditionalFields();
$this
->assertFalse($result);
}
public function testHasConditionalFieldsFormObjectNoDisplay() {
$elementInfo = $this
->createMock(ElementInfoManager::class);
$cfHandlersManager = $this
->createMock(ConditionalFieldsHandlersManager::class);
$sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager);
$sutClass->form = [
'#conditional_fields' => [
'test' => 'test',
],
];
$formState = $this
->createMock(FormState::class);
$formState
->expects($this
->exactly(1))
->method('getFormObject')
->willReturnOnConsecutiveCalls(new \stdClass());
$sutClass->form_state = $formState;
$result = $sutClass
->hasConditionalFields();
$this
->assertFalse($result);
}
public function testHasConditionalFields1Field() {
$elementInfo = $this
->createMock(ElementInfoManager::class);
$cfHandlersManager = $this
->createMock(ConditionalFieldsHandlersManager::class);
$sutClass = new ConditionalFieldsFormHelper($elementInfo, $cfHandlersManager);
$sutClass->form = [
'#conditional_fields' => [
'test' => 'test',
],
];
$formObject = $this
->createMock(NodeForm::class);
$formState = $this
->createMock(FormState::class);
$formState
->expects($this
->exactly(1))
->method('getFormObject')
->willReturnOnConsecutiveCalls($formObject);
$sutClass->form_state = $formState;
$result = $sutClass
->hasConditionalFields();
$this
->assertTrue($result);
}
public function testProcessDependentFields0Fields() {
$sutClass = $this
->createPartialMock(ConditionalFieldsFormHelper::class, [
'processDependeeFields',
'mapStates',
]);
$sutClass
->expects($this
->exactly(0))
->method('processDependeeFields');
$sutClass
->expects($this
->exactly(0))
->method('mapStates');
$conditionalFieldControl = [];
$sutClass->form = [
'#conditional_fields' => $conditionalFieldControl,
'field_src1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
'field_tgt1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
];
$sutClass
->processDependentFields();
$this
->assertSame([
'#conditional_fields' => $conditionalFieldControl,
'field_src1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
'field_tgt1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
], $sutClass->form);
}
public function testProcessDependentFields1Simple() {
$sutClass = $this
->createPartialMock(ConditionalFieldsFormHelper::class, [
'processDependeeFields',
'mapStates',
]);
$sutClass
->expects($this
->exactly(1))
->method('processDependeeFields')
->willReturnOnConsecutiveCalls([
'raw_state',
]);
$sutClass
->expects($this
->exactly(1))
->method('mapStates')
->willReturnMap([
[
[
'raw_state',
],
[
'mapped_state',
],
],
]);
$conditionalFieldControl = [
'field_src1' => [
'parents' => [
'field_src1',
0,
'value',
],
'dependents' => [
'4c6f4ff2-3113-4a75-81b6-164626cf694e' => [
'dependent' => 'field_tgt1',
'options' => [],
],
],
],
'field_tgt1' => [
'field_parents' => [
'field_tgt1',
0,
'value',
],
'dependees' => [
'4c6f4ff2-3113-4a75-81b6-164626cf694e' => [
'dependee' => 'field_src1',
'options' => [],
],
],
],
];
$sutClass->form = [
'#conditional_fields' => $conditionalFieldControl,
'field_src1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
'field_tgt1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
];
$sutClass
->processDependentFields();
$this
->assertSame([
'#conditional_fields' => $conditionalFieldControl,
'field_src1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
'field_tgt1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
'#states' => [
'mapped_state',
],
],
], $sutClass->form);
}
public function testProcessDependeeFields0Fields() {
$sutClass = $this
->createPartialMock(ConditionalFieldsFormHelper::class, [
'getSelector',
'getState',
'elementAddProperty',
'addStateToGroup',
'buildJquerySelectorForField',
'getEffect',
]);
$sutClass
->expects($this
->exactly(0))
->method('getSelector');
$sutClass
->expects($this
->exactly(0))
->method('getState');
$sutClass
->expects($this
->exactly(0))
->method('elementAddProperty');
$sutClass
->expects($this
->exactly(0))
->method('addStateToGroup');
$sutClass
->expects($this
->exactly(0))
->method('buildJquerySelectorForField');
$sutClass
->expects($this
->exactly(0))
->method('getEffect');
$conditionalFieldControl = [];
$sutClass->form = [
'#conditional_fields' => $conditionalFieldControl,
'field_src1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
'field_tgt1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
];
$sutClass->effects = [];
$controllingFields = [];
$targetField = $sutClass->form['field_tgt1'];
$targetFieldLocation = [];
$states = [];
$newStates = $sutClass
->processDependeeFields($controllingFields, $targetField, $targetFieldLocation, $states);
$this
->assertSame([
'#conditional_fields' => $conditionalFieldControl,
'field_src1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
'field_tgt1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
], $sutClass->form);
$this
->assertSame([], $sutClass->effects);
$this
->assertSame([
0 => [
'value' => [
'#type' => 'textfield',
],
],
], $targetField);
$this
->assertSame([], $newStates);
}
public function testProcessDependeeFields1Simple() {
$sutClass = $this
->createPartialMock(ConditionalFieldsFormHelper::class, [
'getSelector',
'getState',
'elementAddProperty',
'addStateToGroup',
'buildJquerySelectorForField',
'getEffect',
]);
$sutClass
->expects($this
->exactly(1))
->method('getSelector')
->willReturnOnConsecutiveCalls('[name="field_src1[0][value]"]');
$sutClass
->expects($this
->exactly(1))
->method('getState')
->willReturnOnConsecutiveCalls([
'!visible' => [
'[name="field_src1[0][value]"]' => [
'value' => 'hideme',
],
],
]);
$sutClass
->expects($this
->exactly(1))
->method('elementAddProperty')
->willReturnOnConsecutiveCalls([
0 => [
'value' => [
'#type' => 'textfield',
],
],
'#element_validate' => [
0 => [
0 => ConditionalFieldsFormHelper::class,
'dependentValidate',
],
],
]);
$sutClass
->expects($this
->exactly(1))
->method('addStateToGroup')
->willReturnOnConsecutiveCalls([
'!visible' => [
'AND' => [
'[name="field_src1[0][value]"]' => [
'value' => 'hideme',
],
],
],
]);
$sutClass
->expects($this
->exactly(1))
->method('buildJquerySelectorForField')
->willReturnOnConsecutiveCalls('#edit-field-tgt1-wrapper');
$sutClass
->expects($this
->exactly(1))
->method('getEffect')
->willReturnOnConsecutiveCalls([]);
$conditionalFieldControl = [
'field_src1' => [
'parents' => [
'field_src1',
0,
'value',
],
'dependents' => [
'4c6f4ff2-3113-4a75-81b6-164626cf694e' => [
'dependent' => 'field_tgt1',
'options' => [],
],
],
],
'field_tgt1' => [
'field_parents' => [
'field_tgt1',
0,
'value',
],
'dependees' => [
'4c6f4ff2-3113-4a75-81b6-164626cf694e' => [
'dependee' => 'field_src1',
'options' => [
'condition' => 'value',
'selector' => '',
'values' => 'hideme',
'grouping' => 'AND',
],
],
],
],
];
$sutClass->form = [
'#conditional_fields' => $conditionalFieldControl,
'field_src1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
'field_tgt1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
];
$sutClass->effects = [];
$controllingFields = $sutClass->form['#conditional_fields']['field_tgt1']['dependees'];
$targetField = $sutClass->form['field_tgt1'];
$targetFieldLocation = $sutClass->form['#conditional_fields']['field_tgt1']['field_parents'];
$states = [];
$newStates = $sutClass
->processDependeeFields($controllingFields, $targetField, $targetFieldLocation, $states);
$this
->assertSame([
'#conditional_fields' => $conditionalFieldControl,
'field_src1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
'field_tgt1' => [
0 => [
'value' => [
'#type' => 'textfield',
],
],
],
], $sutClass->form);
$this
->assertSame([
'#edit-field-tgt1-wrapper' => [],
], $sutClass->effects);
$this
->assertSame([
0 => [
'value' => [
'#type' => 'textfield',
],
],
'#element_validate' => [
0 => [
0 => ConditionalFieldsFormHelper::class,
'dependentValidate',
],
],
], $targetField);
$this
->assertSame([
'!visible' => [
'AND' => [
'[name="field_src1[0][value]"]' => [
'value' => 'hideme',
],
],
],
], $newStates);
}
}