Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(search): order by actor #16433

Merged
merged 5 commits into from Feb 5, 2024

Conversation

Rom1-B
Copy link
Contributor

@Rom1-B Rom1-B commented Jan 25, 2024

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets !31353

For ticket searches, when sorting users by requester, the result did not take anonymous users into account.

The same applies to other types of actor and to changes and problems.

@Rom1-B Rom1-B marked this pull request as ready for review January 25, 2024 15:59
@cedric-anne cedric-anne added this to the 10.0.13 milestone Jan 29, 2024
Copy link
Contributor

@AdrienClairembault AdrienClairembault left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested functionally and I confirm it works perfectly 👍

I am not sure about the tests part tho, it looks very technical as it is checking the exact expected SQL query.

I think having another test a bit more on the functional side (where we add a few actors on a few tickets and sort them using the search engine) would be a great addition.

@trasher
Copy link
Contributor

trasher commented Feb 1, 2024

I totally agree on the tests parts... Current tests does not really check the feature; but it also may hard to achieve properly (I think it's why it's currently done that way :/).

Copy link
Contributor

@AdrienClairembault AdrienClairembault left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test could be simplified by using a data provider.
This way we can easily add new cases in the future :)

Suggestion:

diff --git a/tests/functional/Search.php b/tests/functional/Search.php
index a4d671940b..06bd0d13b5 100644
--- a/tests/functional/Search.php
+++ b/tests/functional/Search.php
@@ -1511,7 +1511,10 @@ class Search extends DbTestCase
                             )) DESC ");
     }
 
-    public function testAddOrderByUser()
+    /**
+     * Data provider for testAddOrderByUser
+     */
+    protected function testAddOrderByUserProvider(): iterable
     {
         $this->login('glpi', 'glpi');
 
@@ -1520,7 +1523,7 @@ class Search extends DbTestCase
         $group_1 = getItemByTypeName('Group', '_test_group_1')->getID();
 
         // Creates Changes with different requesters
-        $t = $this->createItems('Change', [
+        $this->createItems('Change', [
             // Test set on requester
             [
                 'name' => 'testAddOrderByUser user 1 (R)',
@@ -1579,105 +1582,64 @@ class Search extends DbTestCase
             ],
         ]);
 
-        // get the Changes filtered title containing 'testAddOrderByUser' and ordered by requester name in ASC
-        $search_params = [
-            'is_deleted' => 0,
-            'start' => 0,
-            'criteria[0][field]' => 1,
-            'criteria[0][searchtype]' => 'contains',
-            'criteria[0][value]' => 'testAddOrderByUser',
-            'sort' => 4,
-            'order' => 'ASC',
+        yield [
+            'search_params' => [
+                'is_deleted' => 0,
+                'start' => 0,
+                'criteria[0][field]' => 1,
+                'criteria[0][searchtype]' => 'contains',
+                'criteria[0][value]' => 'testAddOrderByUser',
+                'sort' => 4,
+                'order' => 'ASC',
+            ],
+            'expected_order' => [
+                'testAddOrderByUser group 1 (R)',              //  no requester
+                'testAddOrderByUser user 1 (R)',               //  _test_user
+                'testAddOrderByUser user 1 (R) + group 1 (R)', //  _test_user
+                'testAddOrderByUser user 1 (R) + user 2 (R)',  //  _test_user, glpi
+                'testAddOrderByUser user 2 (R)',               //  glpi
+                'testAddOrderByUser anonymous user (R)',       //  myemail@email.com
+            ]
         ];
-        $data = $this->doSearch('Change', $search_params);
 
-        $this->integer($data['data']['totalcount'])->isEqualTo(6);
-        $this->array($data)
-            ->array['data']
-            ->array['rows']
-            ->array[0]
-            ->array['raw']
-            ->string['ITEM_Change_1']->isEqualTo('testAddOrderByUser group 1 (R)');
-        $this->array($data)
-            ->array['data']
-            ->array['rows']
-            ->array[1]
-            ->array['raw']
-            ->string['ITEM_Change_1']->isEqualTo('testAddOrderByUser user 1 (R)');
-        $this->array($data)
-            ->array['data']
-            ->array['rows']
-            ->array[2]
-            ->array['raw']
-            ->string['ITEM_Change_1']->isEqualTo('testAddOrderByUser user 1 (R) + group 1 (R)');
-        $this->array($data)
-            ->array['data']
-            ->array['rows']
-            ->array[3]
-            ->array['raw']
-            ->string['ITEM_Change_1']->isEqualTo('testAddOrderByUser user 1 (R) + user 2 (R)');
-        $this->array($data)
-            ->array['data']
-            ->array['rows']
-            ->array[4]
-            ->array['raw']
-            ->string['ITEM_Change_1']->isEqualTo('testAddOrderByUser user 2 (R)');
-        $this->array($data)
-            ->array['data']
-            ->array['rows']
-            ->array[5]
-            ->array['raw']
-            ->string['ITEM_Change_1']->isEqualTo('testAddOrderByUser anonymous user (R)');
-
-        // get the Changes filtered title containing 'testAddOrderByUser' and sort by requester name in DESC
-        $search_params = [
-            'is_deleted' => 0,
-            'start' => 0,
-            'criteria[0][field]' => 1,
-            'criteria[0][searchtype]' => 'contains',
-            'criteria[0][value]' => 'testAddOrderByUser',
-            'sort' => 4,
-            'order' => 'DESC',
+        yield [
+            'search_params' => [
+                'is_deleted' => 0,
+                'start' => 0,
+                'criteria[0][field]' => 1,
+                'criteria[0][searchtype]' => 'contains',
+                'criteria[0][value]' => 'testAddOrderByUser',
+                'sort' => 4,
+                'order' => 'DESC',
+            ],
+            'expected_order' => [
+                'testAddOrderByUser anonymous user (R)',       //  myemail@email.com
+                'testAddOrderByUser user 2 (R)',               //  glpi
+                'testAddOrderByUser user 1 (R) + user 2 (R)',  //  _test_user, glpi
+                'testAddOrderByUser user 1 (R)',               //  _test_user
+                'testAddOrderByUser user 1 (R) + group 1 (R)', //  _test_user
+                'testAddOrderByUser group 1 (R)',              //  no requester
+            ]
         ];
+    }
+
+    /**
+     * @dataProvider testAddOrderByUserProvider
+     */
+    public function testAddOrderByUser(
+        array $search_params,
+        array $expected_order
+    ) {
         $data = $this->doSearch('Change', $search_params);
 
-        $this->integer($data['data']['totalcount'])->isEqualTo(6);
-        $this->array($data)
-            ->array['data']
-            ->array['rows']
-            ->array[0]
-            ->array['raw']
-            ->string['ITEM_Change_1']->isEqualTo('testAddOrderByUser anonymous user (R)');
-        $this->array($data)
-            ->array['data']
-            ->array['rows']
-            ->array[1]
-            ->array['raw']
-            ->string['ITEM_Change_1']->isEqualTo('testAddOrderByUser user 2 (R)');
-        $this->array($data)
-            ->array['data']
-            ->array['rows']
-            ->array[2]
-            ->array['raw']
-            ->string['ITEM_Change_1']->isEqualTo('testAddOrderByUser user 1 (R) + user 2 (R)');
-        $this->array($data)
-            ->array['data']
-            ->array['rows']
-            ->array[3]
-            ->array['raw']
-            ->string['ITEM_Change_1']->isEqualTo('testAddOrderByUser user 1 (R)');
-        $this->array($data)
-            ->array['data']
-            ->array['rows']
-            ->array[4]
-            ->array['raw']
-            ->string['ITEM_Change_1']->isEqualTo('testAddOrderByUser user 1 (R) + group 1 (R)');
-        $this->array($data)
-            ->array['data']
-            ->array['rows']
-            ->array[5]
-            ->array['raw']
-            ->string['ITEM_Change_1']->isEqualTo('testAddOrderByUser group 1 (R)');
+        // Extract items names
+        $items = [];
+        foreach ($data['data']['rows'] as $row) {
+            $items[] = $row['raw']['ITEM_Change_1'];
+        }
+
+        // Validate order
+        $this->array($items)->isEqualTo($expected_order);
     }
 
     private function cleanSQL($sql)

@cedric-anne cedric-anne merged commit 7e05bc0 into glpi-project:10.0/bugfixes Feb 5, 2024
13 checks passed
@Rom1-B Rom1-B deleted the support_31353 branch February 5, 2024 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants