0

The overarching question

How do I get the contents of the PHP-variable: COOKIEHASH inside a Cypress-test.


I'm writing some Cypress-test for WordPress, and in order to set the cookies to log in a user in WordPress using Cypress, then I need the contents of the COOKIEHASH-variable.

For those not familiar with the COOKIEHASH, go to a WordPress-installation and put in this code anywhere:

echo '<pre>';
print_r(COOKIEHASH);
echo '</pre>';
die();

And then you'll see it. It looks something like this: a8b94154380982c3284a467b8aa224c6.

When I run my Cypress-tests for the first time, then I don't know what that hash is, so I don't know which cookies to set.

So currently I log in manually, go to cookies in my browser and get the hash. Which is quite the manual operation for automated tests. :-)


My attempts / thoughts

  1. It looks like an MD5-hash. But I can't recreate the cookie-hash with an JS MD5-function. It doesn't correspond. I haven't tried that many MD5-JS-functions, it should be said.

If I hash zeth.dk, then the js-md5-hash gives me: 8aa4b9bbc2a9e946b670fd8372a3081a
and I can see in my cookies that the hash is: b7d8d5a26884619e3b3b4481ba778642

  1. I considered making an API-endpoint (in my functions.php), that returns the cookie-hash. But this just feels wrong, making my code to backflips like this.
5
  • What did you try hashing? It's the site URL. Or you could define your own fixed COOKIEHASH in wp-config instead. But it shouldn't change for a given site, so even if you do have to do it once manually that shouldn't be a problem?
    – Rup
    Oct 23, 2020 at 16:31
  • Looking at your Cypress tests on your other question, you're logging in properly once to get the cookies set then saving the values from there. Couldn't you just save any cookie that matches wordpress_* automatically? and/or find one of them and extract the hash from that.
    – Rup
    Oct 23, 2020 at 16:57
  • Thanks for weighing in, @Rup . I updated the question with examples of the JS-MD5 that I used. I tried several and none of the matched. Regarding saving any cookies that matches wordpress_*, then I played around with that for ages. I experienced som wierd bugs. I tried using preserve with a regex, as you suggests - but I just couldn't get it to work. On paper, - what you mention is right. But I just couldn't get it to work. So this is my extensive work-around (that works). :-)
    – Zeth
    Oct 23, 2020 at 19:07
  • Try hashing https://zeth.dk - I think that works
    – Rup
    Oct 23, 2020 at 19:10
  • Oh, God. You're right! I think I must have messed up http/https for the sites I checked. If you write your comment as an answer, I can mark it as the answer.
    – Zeth
    Oct 23, 2020 at 19:37

1 Answer 1

2

By default it's the MD5 hash of the site URL:

    $siteurl = get_site_option( 'siteurl' );
    if ( $siteurl ) {
        define( 'COOKIEHASH', md5( $siteurl ) );
    }

This includes the scheme but not a trailing slash, so in your example you'd need the hash of https://zeth.dk.

You can also override this by defining COOKIEHASH in wp-config.php with some other value.

2
  • Interesting. Mine does include the trailing slash, as does the result of get_site_option( 'siteurl' );.
    – Pat J
    Oct 23, 2020 at 21:23
  • 1
    @PatJ Oh, OK - I guess it just uses the configured value as-is then? I thought WordPress removed the trailing slash somewhere, I guess not.
    – Rup
    Oct 23, 2020 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.