- Issue created by @jasonawant
- π¦πΊAustralia mstrelan
In a client project I have a trait like this:
<?php declare(strict_types=1); namespace Drupal\Tests\my_module; use GuzzleHttp\Cookie\CookieJar; use Symfony\Component\BrowserKit\CookieJar as BrowserKitCookieJar; trait SessionCookiesTestTrait { public function getSessionCookies(): CookieJar { /** @var \Behat\Mink\Driver\BrowserKitDriver $driver */ $driver = $this->getSession()->getDriver(); $cookieJar = $driver->getClient()->getCookieJar(); $this->assertInstanceOf(BrowserKitCookieJar::class, $cookieJar); $domain = \parse_url($this->baseUrl, PHP_URL_HOST); \assert(\is_string($domain)); return CookieJar::fromArray( $cookieJar->allRawValues($this->baseUrl), $domain, ); } }
Usage example:
\Drupal::service('http_client')->request('POST', $this->buildUrl($url), [ 'cookies' => $this->getSessionCookies(), 'form_params' => ['ids' => $ids, 'tokens' => $tokens], 'http_errors' => FALSE, ]);
Would something like that work for you? Would it make sense to include this in DTT?
- πΊπΈUnited States jasonawant New Orleans, USA
Thanks mstrelan!
I think I'm missing something. How does this get the user session cookie?
Prior to the example usage, would you use UserCreationTrait::setUpCurrentUser() to create use and set as current user?
- π¦πΊAustralia mstrelan
Sorry I omitted that. Essentially use
\weitzman\DrupalTestTraits\Entity\UserCreationTrait::createUser
and\Drupal\Tests\UiHelperTrait::drupalLogin
.Example:
$user = $this->createUser(); $user->addRole('MY_ROLE_ID'); $user->save(); $this->drupalLogin($user);
- πΊπΈUnited States jasonawant New Orleans, USA
Thank you @mstrelan! This worked for my use case.
I don't think its quite necessary to consider adding this to DTT, but perhaps if the request comes up again, maybe.
Automatically closed - issue fixed for 2 weeks with no activity.