Browse Source

FIX Skip adding tokens when the method is GET and the data field is empty

FIX if statement and code quality

Update IntegrationTestTrait.php

CS Fix

Add test for IntegrationTestTraitTest.php for GET method tokens

Quality fix for IntegrationTestTraitTest.php
KrzysiekNDS 2 years ago
parent
commit
74f3027fbc

+ 3 - 1
src/TestSuite/IntegrationTestTrait.php

@@ -643,7 +643,9 @@ trait IntegrationTestTrait
         ) {
             $props['input'] = http_build_query($data);
         } else {
-            $data = $this->_addTokens($tokenUrl, $data);
+            if ($method !== 'GET' || !empty($data)) {
+                $data = $this->_addTokens($tokenUrl, $data);
+            }
             $props['post'] = $this->_castToString($data);
         }
 

+ 22 - 0
tests/TestCase/TestSuite/IntegrationTestTraitTest.php

@@ -233,6 +233,28 @@ class IntegrationTestTraitTest extends TestCase
     }
 
     /**
+     * Test for issue #17612 - skip adding tokens for GET without data.
+     */
+    public function testAddTokenInGetRequest(): void
+    {
+        $this->enableCsrfToken();
+        $this->enableSecurityToken();
+        $requestWithoutTokens = $this->_buildRequest('tasks/view', 'GET');
+
+        $this->assertArrayNotHasKey('_Token', $requestWithoutTokens['post']);
+        $this->assertArrayNotHasKey('_csrfToken', $requestWithoutTokens['post']);
+        $this->assertArrayNotHasKey('csrfToken', $requestWithoutTokens['cookies']);
+
+        $this->enableCsrfToken();
+        $this->enableSecurityToken();
+        $requestWithTokens = $this->_buildRequest('tasks/view', 'GET', ['lorem' => 'ipsum']);
+
+        $this->assertArrayHasKey('_Token', $requestWithTokens['post']);
+        $this->assertArrayHasKey('_csrfToken', $requestWithTokens['post']);
+        $this->assertArrayHasKey('csrfToken', $requestWithTokens['cookies']);
+    }
+
+    /**
      * Test building a request, with query parameters
      */
     public function testRequestBuildingQueryParameters(): void