|
9 | 9 | use Drupal\Core\Form\FormStateInterface;
|
10 | 10 | use Drupal\data_dictionary_widget\Indexes\IndexFieldAddCreation;
|
11 | 11 | use Drupal\data_dictionary_widget\Indexes\IndexFieldCallbacks;
|
| 12 | +use Drupal\data_dictionary_widget\Indexes\IndexFieldEditCreation; |
12 | 13 | use Drupal\data_dictionary_widget\Indexes\IndexFieldOperations;
|
| 14 | +use Drupal\data_dictionary_widget\Indexes\IndexValidation; |
13 | 15 | use Drupal\data_dictionary_widget\Plugin\Field\FieldWidget\DataDictionaryWidget;
|
14 | 16 | use PHPUnit\Framework\TestCase;
|
15 | 17 |
|
@@ -341,7 +343,7 @@ public function testEditIndexesButtonsCreationDictionaryWidget() {
|
341 | 343 | }
|
342 | 344 |
|
343 | 345 | /**
|
344 |
| - * Test edit a data dictionary index field and save it. |
| 346 | + * Test edit a data dictionary index and save it. |
345 | 347 | */
|
346 | 348 | public function testEditDataDictionaryIndexDictionaryWidget() {
|
347 | 349 | $formState = $this->createMock(FormStateInterface::class);
|
@@ -519,4 +521,259 @@ public function testEditDataDictionaryIndexDictionaryWidget() {
|
519 | 521 | $this->assertEquals($updated_index[0]["type"], $element["indexes"]["data"]["#rows"][0]["type"]);
|
520 | 522 | }
|
521 | 523 |
|
| 524 | + /** |
| 525 | + * Test edit index for indexes. |
| 526 | + */ |
| 527 | + public function testEditDataDictionaryIndexEdit() { |
| 528 | + // Arrange |
| 529 | + $indexKey = 'index_key_0'; |
| 530 | + $current_index = [ |
| 531 | + [ |
| 532 | + 'description' => 'test', |
| 533 | + 'type' => 'index', |
| 534 | + 'fields' => [ |
| 535 | + [ |
| 536 | + 'name' => 'test', |
| 537 | + 'length' => 20 |
| 538 | + ] |
| 539 | + ] |
| 540 | + ] |
| 541 | + ]; |
| 542 | + $index_being_modified = $current_index; |
| 543 | + $formState = $this->createMock(FormStateInterface::class); |
| 544 | + $formState->expects($this->any()) |
| 545 | + ->method('get') |
| 546 | + ->willReturnOnConsecutiveCalls(FALSE); |
| 547 | + |
| 548 | + // Act |
| 549 | + $edit_index = IndexFieldEditCreation::editIndex($indexKey, $current_index, $index_being_modified, $formState); |
| 550 | + |
| 551 | + // Assert |
| 552 | + $this->assertNotNull($edit_index); |
| 553 | + $this->assertEquals('field_json_metadata[0][indexes][edit_index][index_key_0][description]', $edit_index['description']['#name']); |
| 554 | + $this->assertEquals($current_index[0]['description'], $edit_index['description']['#value']); |
| 555 | + $this->assertEquals('field_json_metadata[0][indexes][edit_index][index_key_0][type]', $edit_index['type']['#name']); |
| 556 | + $this->assertEquals($current_index[0]['type'], $edit_index['type']['#value']); |
| 557 | + $this->assertEquals($current_index[0]['fields'][0]['name'], $edit_index['group']['fields']['data']['#rows'][0]['name']); |
| 558 | + $this->assertEquals($current_index[0]['fields'][0]['length'], $edit_index['group']['fields']['data']['#rows'][0]['length']); |
| 559 | + } |
| 560 | + |
| 561 | + /** |
| 562 | + * Test edit index fields for indexes. |
| 563 | + */ |
| 564 | + public function testEditDataDictionaryIndexEditFields() { |
| 565 | + // Arrange |
| 566 | + $indexKey = 'index_field_key_0'; |
| 567 | + $current_index_fields = [ |
| 568 | + [ |
| 569 | + 'name' => 'test', |
| 570 | + 'length' => 20 |
| 571 | + ] |
| 572 | + ]; |
| 573 | + |
| 574 | + // Act |
| 575 | + $edit_index_fields = IndexFieldEditCreation::editIndexFields($indexKey, $current_index_fields, null); |
| 576 | + |
| 577 | + // Assert |
| 578 | + $this->assertNotNull($edit_index_fields); |
| 579 | + $this->assertEquals('field_json_metadata[0][indexes][fields][edit_index_fields][0][name]', $edit_index_fields['name']['#name']); |
| 580 | + $this->assertEquals($current_index_fields[0]['name'], $edit_index_fields['name']['#value']); |
| 581 | + $this->assertEquals('field_json_metadata[0][indexes][fields][edit_index_fields][0][length]', $edit_index_fields['length']['#name']); |
| 582 | + $this->assertEquals($current_index_fields[0]['length'], $edit_index_fields['length']['#value']); |
| 583 | + } |
| 584 | + |
| 585 | + /** |
| 586 | + * Test editing data dictionary index fields and save it. |
| 587 | + */ |
| 588 | + public function testEditDataDictionaryIndexFieldsDictionaryWidget() { |
| 589 | + $formState = $this->createMock(FormStateInterface::class); |
| 590 | + $formObject = $this->createMock(EntityFormInterface::class); |
| 591 | + $entity = $this->createMock(FieldableEntityInterface::class); |
| 592 | + $fieldItemList = $this->createMock(FieldItemListInterface::class); |
| 593 | + $field_definition = $this->createMock(FieldDefinitionInterface::class); |
| 594 | + $settings = []; |
| 595 | + $third_party_settings = []; |
| 596 | + $form = []; |
| 597 | + $plugin_id = ''; |
| 598 | + $plugin_definition = []; |
| 599 | + |
| 600 | + $current_index_field = [ |
| 601 | + [ |
| 602 | + 'name' => 'test', |
| 603 | + 'length' => 20, |
| 604 | + ] |
| 605 | + ]; |
| 606 | + |
| 607 | + $updated_index_field = [ |
| 608 | + [ |
| 609 | + 'name' => 'test_update', |
| 610 | + 'length' => 25, |
| 611 | + ] |
| 612 | + ]; |
| 613 | + |
| 614 | + $user_input = [ |
| 615 | + 'field_json_metadata' => [ |
| 616 | + 0 => [ |
| 617 | + 'identifier' => 'test_identifier', |
| 618 | + 'title' => 'test_title', |
| 619 | + 'indexes' => [ |
| 620 | + 'edit_index' => [ |
| 621 | + 'index_key_0' => [ |
| 622 | + 'description' => 'test_edit', |
| 623 | + 'type' => 'fulltext', |
| 624 | + ] |
| 625 | + ], |
| 626 | + 'fields' => [ |
| 627 | + 'edit_index_fields' => [ |
| 628 | + [ |
| 629 | + 'name' => 'test', |
| 630 | + 'length' => 20, |
| 631 | + ] |
| 632 | + ] |
| 633 | + ] |
| 634 | + ], |
| 635 | + ], |
| 636 | + ], |
| 637 | + ]; |
| 638 | + |
| 639 | + $form["field_json_metadata"]["widget"][0] = [ |
| 640 | + "dictionary_fields" => [ |
| 641 | + "data" => [ |
| 642 | + "#rows" => [ |
| 643 | + 0 => [], |
| 644 | + ], |
| 645 | + ], |
| 646 | + ], |
| 647 | + 'indexes' => [ |
| 648 | + 'data' => [ |
| 649 | + "#rows" => [ |
| 650 | + [ |
| 651 | + 'description' => 'test', |
| 652 | + 'type' => 'index', |
| 653 | + 'fields' => [ |
| 654 | + 'name' => 'test', |
| 655 | + 'length' => 20, |
| 656 | + ], |
| 657 | + ], |
| 658 | + ], |
| 659 | + ], |
| 660 | + 'fields' => [ |
| 661 | + 'data' => [ |
| 662 | + "#rows" => [ |
| 663 | + 0 => [ |
| 664 | + 'name' => 'test', |
| 665 | + 'length' => 20, |
| 666 | + ], |
| 667 | + ], |
| 668 | + ], |
| 669 | + ] |
| 670 | + ], |
| 671 | + ]; |
| 672 | + |
| 673 | + $formState->expects($this->exactly(2)) |
| 674 | + ->method('getFormObject') |
| 675 | + ->willReturn($formObject); |
| 676 | + |
| 677 | + $formObject->expects($this->exactly(2)) |
| 678 | + ->method('getEntity') |
| 679 | + ->willReturn($entity); |
| 680 | + |
| 681 | + $entity->expects($this->exactly(2)) |
| 682 | + ->method('set') |
| 683 | + ->with('field_data_type', 'data-dictionary'); |
| 684 | + |
| 685 | + // The gets are happening in indexEditCallback and formElement and function calls in it that use the formState variable. |
| 686 | + // Set the value for various $form_state calls that use the current index and then are updated when we pass the updated index value. |
| 687 | + $formState->expects($this->any()) |
| 688 | + ->method('get') |
| 689 | + ->willReturnOnConsecutiveCalls( |
| 690 | + $current_index_field, |
| 691 | + $current_index_field, |
| 692 | + [], |
| 693 | + [], |
| 694 | + $current_index_field, |
| 695 | + [], |
| 696 | + [], |
| 697 | + [], |
| 698 | + [], |
| 699 | + [], |
| 700 | + [], |
| 701 | + $current_index_field, |
| 702 | + $current_index_field, |
| 703 | + [], |
| 704 | + $current_index_field, |
| 705 | + [], |
| 706 | + [], |
| 707 | + TRUE, |
| 708 | + $current_index_field, |
| 709 | + $current_index_field, |
| 710 | + [], |
| 711 | + [], |
| 712 | + [], |
| 713 | + [], |
| 714 | + [], |
| 715 | + [], |
| 716 | + [], |
| 717 | + [], |
| 718 | + [], |
| 719 | + $updated_index_field, |
| 720 | + $updated_index_field, |
| 721 | + [], |
| 722 | + $updated_index_field, |
| 723 | + ); |
| 724 | + |
| 725 | + $formState->expects($this->any()) |
| 726 | + ->method('getTriggeringElement') |
| 727 | + ->willReturnOnConsecutiveCalls( |
| 728 | + ['#op' => 'edit_index_field_key_0'], ['#op' => 'edit_index_field_key_0'], |
| 729 | + ['#op' => 'update_index_field_key_0'], ['#op' => 'update_index_field_key_0'], |
| 730 | + ); |
| 731 | + |
| 732 | + $formState->expects($this->any()) |
| 733 | + ->method('getUserInput') |
| 734 | + ->willReturn($user_input); |
| 735 | + |
| 736 | + $dataDictionaryWidget = new DataDictionaryWidget( |
| 737 | + $plugin_id, |
| 738 | + $plugin_definition, |
| 739 | + $field_definition, |
| 740 | + $settings, |
| 741 | + $third_party_settings |
| 742 | + ); |
| 743 | + |
| 744 | + // Trigger callback function to edit fields. |
| 745 | + IndexFieldCallbacks::indexEditSubformCallback($form, $formState); |
| 746 | + |
| 747 | + // First call to re-create data dictionary form with the editable fields. |
| 748 | + $element = $dataDictionaryWidget->formElement( |
| 749 | + $fieldItemList, |
| 750 | + 0, |
| 751 | + [], |
| 752 | + $form, |
| 753 | + $formState |
| 754 | + ); |
| 755 | + |
| 756 | + // Assert edit feature loads form with the current field values. |
| 757 | + $this->assertNotNull($element); |
| 758 | + $this->assertEquals($current_index_field[0]["name"], $element["indexes"]["fields"]["data"]["#rows"][0]["name"]); |
| 759 | + $this->assertEquals($current_index_field[0]["length"], $element["indexes"]["fields"]["data"]["#rows"][0]["length"]); |
| 760 | + |
| 761 | + // Trigger callback function to save the edited fields. |
| 762 | + IndexFieldCallbacks::indexEditSubformCallback($form, $formState); |
| 763 | + |
| 764 | + // Second call to re-create data dictionary and apply the edits made to the fields. |
| 765 | + $element = $dataDictionaryWidget->formElement( |
| 766 | + $fieldItemList, |
| 767 | + 0, |
| 768 | + [], |
| 769 | + $form, |
| 770 | + $formState |
| 771 | + ); |
| 772 | + |
| 773 | + // Assert update feature loads form with the edited field values. |
| 774 | + $this->assertNotNull($element); |
| 775 | + $this->assertEquals($updated_index_field[0]["name"], $element["indexes"]["fields"]["data"]["#rows"][0]["name"]); |
| 776 | + $this->assertEquals($updated_index_field[0]["length"], $element["indexes"]["fields"]["data"]["#rows"][0]["length"]); |
| 777 | + } |
| 778 | + |
522 | 779 | }
|
0 commit comments