|
37 | 37 | use CommonDBTM;
|
38 | 38 | use Contract;
|
39 | 39 | use DbTestCase;
|
| 40 | +use Location; |
| 41 | +use MassiveAction; |
40 | 42 | use Notepad;
|
41 | 43 | use Problem;
|
42 | 44 | use Session;
|
@@ -160,6 +162,7 @@ protected function processMassiveActionsForOneItemtype(
|
160 | 162 | ->getMock();
|
161 | 163 |
|
162 | 164 | // Mock needed methods
|
| 165 | + $ma->POST = $input; |
163 | 166 | $ma->method('getAction')->willReturn($action_code);
|
164 | 167 | $ma->method('addMessage')->willReturn(null);
|
165 | 168 | $ma->method('getInput')->willReturn($input);
|
@@ -699,4 +702,124 @@ public function testaveSearchSpecificmassiveAction()
|
699 | 702 | );
|
700 | 703 | }
|
701 | 704 | }
|
| 705 | + |
| 706 | + public function testProcessMassiveActionsForOneItemtype_updateDropdownOrCommonDBConnexity() |
| 707 | + { |
| 708 | + $this->login(); |
| 709 | + |
| 710 | + $provider = $this->updateDropdownOrCommonDBConnexityProvider(); |
| 711 | + foreach ($provider as $row) { |
| 712 | + $item = $row['item']; |
| 713 | + $input = $row['input']; |
| 714 | + $has_right = $row['has_right']; |
| 715 | + $should_work = $row['should_work']; |
| 716 | + |
| 717 | + |
| 718 | + $old_session = $_SESSION['glpiactiveprofile'][Ticket::$rightname] ?? 0; |
| 719 | + if ($has_right) { |
| 720 | + $_SESSION['glpiactiveprofile'][Ticket::$rightname] = UPDATE; |
| 721 | + } else { |
| 722 | + $_SESSION['glpiactiveprofile'][Ticket::$rightname] = 0; |
| 723 | + } |
| 724 | + |
| 725 | + |
| 726 | + |
| 727 | + // Check rights set up was successful |
| 728 | + $this->assertSame( |
| 729 | + $has_right, |
| 730 | + (bool) Session::haveRight(Ticket::$rightname, UPDATE) |
| 731 | + ); |
| 732 | + |
| 733 | + // Default expectation: can't run |
| 734 | + $expected_ok = 0; |
| 735 | + $expected_ko = 0; |
| 736 | + |
| 737 | + // Update expectation: this item should be OK |
| 738 | + if ($should_work) { |
| 739 | + $expected_ok = 1; |
| 740 | + } else { |
| 741 | + $expected_ko = 1; |
| 742 | + } |
| 743 | + |
| 744 | + // Execute action |
| 745 | + $this->processMassiveActionsForOneItemtype( |
| 746 | + "update", |
| 747 | + $item, |
| 748 | + [$item->fields['id']], |
| 749 | + $input, |
| 750 | + $expected_ok, |
| 751 | + $expected_ko, |
| 752 | + MassiveAction::class |
| 753 | + ); |
| 754 | + |
| 755 | + // Reset rights |
| 756 | + $_SESSION['glpiactiveprofile'][Ticket::$rightname] = $old_session; |
| 757 | + } |
| 758 | + } |
| 759 | + |
| 760 | + protected function updateDropdownOrCommonDBConnexityProvider() |
| 761 | + { |
| 762 | + $ticket = new Ticket(); |
| 763 | + $location = new Location(); |
| 764 | + $id = $ticket->add([ |
| 765 | + 'name' => 'test', |
| 766 | + 'content' => 'test', |
| 767 | + 'entities_id' => getItemByTypeName('Entity', '_test_root_entity', true), |
| 768 | + ]); |
| 769 | + $ticket->getFromDB($id); |
| 770 | + $this->assertGreaterThan(0, $id); |
| 771 | + $this->assertSame($ticket->fields['entities_id'], getItemByTypeName('Entity', '_test_root_entity', true)); |
| 772 | + |
| 773 | + $this->createItem( |
| 774 | + \Entity::class, |
| 775 | + [ |
| 776 | + 'name' => '_test_root_subentity', |
| 777 | + 'entities_id' => getItemByTypeName('Entity', '_test_root_entity', true), |
| 778 | + ] |
| 779 | + ); |
| 780 | + |
| 781 | + $location = new Location(); |
| 782 | + $location_id_1 = $location->add([ |
| 783 | + 'name' => 'test', |
| 784 | + 'entities_id' => getItemByTypeName('Entity', '_test_root_entity', true), |
| 785 | + ]); |
| 786 | + $this->assertGreaterThan(0, $location_id_1); |
| 787 | + |
| 788 | + $location_id_2 = $location->add([ |
| 789 | + 'name' => 'test_sub', |
| 790 | + 'entities_id' => getItemByTypeName('Entity', '_test_root_subentity', true), |
| 791 | + ]); |
| 792 | + $this->assertGreaterThan(0, $location_id_2); |
| 793 | + |
| 794 | + return [ |
| 795 | + [ |
| 796 | + // Should work |
| 797 | + 'item' => $ticket, |
| 798 | + 'input' => [ |
| 799 | + 'locations_id' => $location_id_1, |
| 800 | + 'search_options' => |
| 801 | + [ |
| 802 | + $ticket->getType() => 83, |
| 803 | + ], |
| 804 | + 'field' => 'locations_id', |
| 805 | + ], |
| 806 | + 'has_right' => true, |
| 807 | + 'should_work' => true, |
| 808 | + ], |
| 809 | + [ |
| 810 | + // Should not work |
| 811 | + 'item' => $ticket, |
| 812 | + 'input' => [ |
| 813 | + 'locations_id' => $location_id_2, |
| 814 | + 'search_options' => |
| 815 | + [ |
| 816 | + $ticket->getType() => 83, |
| 817 | + ], |
| 818 | + 'field' => 'locations_id', |
| 819 | + ], |
| 820 | + 'has_right' => true, |
| 821 | + 'should_work' => false, //not same entity |
| 822 | + ], |
| 823 | + ]; |
| 824 | + } |
702 | 825 | }
|
0 commit comments