Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
See #2735005: Convert all Simpletest web tests to BrowserTestBase (or UnitTestBase/KernelTestBase).
This will also involve converting cURL calls to Guzzle because cURL is not compatible with BTB.
\Drupal\history\tests\HistoryTest
has a couple of curlExec
calls, which are:
a) hard to read
b) not compatible with BTB
Proposed resolution
Move it to use guzzle directly.
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#23 | 2795049-23.patch | 6.88 KB | klausi |
Comments
Comment #2
dawehnerComment #3
legovaerI have been working on this issue as you can see in the attached patch. Please have a look at my progress so far. It's not yet finished by for some reason I keep getting a 403 when executing
getNodeReadTimestamps()
.Anything I'm missing here?
Also: do we have other examples?
Comment #4
legovaerPatch in #3 contained changes that weren't related to this issue.
Comment #5
borisson_You created a
protected $client
but weren't using it in the code. So I changed that. No idea why the 403 is happening.Comment #8
jofitz CreditAttribution: jofitz at ComputerMinds commentedThe 403 is happening because the Guzzle request is from an unauthenticated (anon) user. After 5+ hours investigating this I'm not much closer to finding the solution, but at least we now know the problem!
I suspect the key is to pass cookies along with the body and headers (see patch), but I can't find a method to create a valid CookieJar. There's no point running this past the testbot - I can guarantee it will fail.
I think there is also an issue with the format of the node_ids being posted, because the call fails even when the user restrictions are removed from getNodeReadTimestamps().
Comment #9
jofitz CreditAttribution: jofitz at ComputerMinds commentedUpdated title and summary to refer to the parent issue, #2735005: Convert all Simpletest web tests to BrowserTestBase (or UnitTestBase/KernelTestBase).
Comment #10
jofitz CreditAttribution: jofitz at ComputerMinds commentedComment #11
klausiI think this setup is not necessary. We already have a Guzzle client in the Mink driver we are using for browser tests. $this->getSession()->getDriver()->getClient()->getClient(); should give you the Guzzle client, right?
We really need an ApiTestBase for such cases, see #2812967: API test base: Provide a test helper to test REST and similar APIs.
Not sure what is better for this issue, instantiating a new Guzzle client or reusing the existing one from Mink.
Comment #12
jofitz CreditAttribution: jofitz at ComputerMinds commentedHow about this then? It now uses the existing Guzzle client from Mink rather than instantiating a new one.
The cookies are the vital thing that means the Guzzle calls are successful. The could (probably) be obtained for each Guzzle request individually, but it is more efficient to do so in the set-up.
FYI some of the functionality is based on that in modules/system/tests/src/Functional/CsrfRequestHeaderTest.php.
Comment #13
dawehnerWe could quickly add a todo pointing to this other issue.
Comment #14
boaloysius CreditAttribution: boaloysius as a volunteer commentedThe patch in #14 don't apply to https://www.drupal.org/project/drupal/releases/8.4.x-dev I downloaded today (7th march). I am uploading the updated patch.
Comment #15
boaloysius CreditAttribution: boaloysius as a volunteer commentedI am doing my first reroll. In #14 i did it manually. In this I use git.
Comment #17
dawehnerLet's move this code onto a getHttpClient method.
Are you sure you have to specify the cookies? The client should have the information already, at least according to your setup
Its nice how the code is a bit easier to read than the weird curl stuff.
Comment #18
jofitz CreditAttribution: jofitz at ComputerMinds commentedAlso added the @see mentioned in #13.
Comment #19
dawehnerDid you tried using \Drupal::service('http_client_factory')->fromOptions(['cookies' => $this->cookies']); I could imagine that this would help.
Comment #20
jofitz CreditAttribution: jofitz at ComputerMinds commentedYes, I used
$this->client = \Drupal::service('http_client_factory')->fromOptions($opts);
in #10, but @klausi advised using$this->getSession()->getDriver()->getClient()->getClient();
instead in #11.Comment #21
klausiThe cookies are ugly, I tried some variations myself with setting it on the Guzzle client but it also didn't work for me. We might find something better in the future, but for now this is already a great improvement and even makes the test code much more readable.
doc block is wrong, this is not he client. Should be "The cookie jar holding the testing session cookies for Guzzle requests."
Description missing, should be "The client with BrowserTestBase configuration."
Comment #22
jofitz CreditAttribution: jofitz at ComputerMinds commentedComment #23
klausiLooks good!
Uploading the same patch created with "git diff -M5%" to prove that the file has moved.
Comment #24
alexpottCommitted and pushed fce944d to 8.4.x and af000d8 to 8.3.x. Thanks!
Comment #27
boaloysius CreditAttribution: boaloysius as a volunteer and at Google Summer of Code commentedComment #29
andypostFollow-up to convert remaining test #2898437: Convert HistoryTimestampTest to kernel test