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
Let's add commands to login/logout in nightwatch based tests.
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#39 | 2973879-37.patch | 26.27 KB | lauriii |
Comments
Comment #2
samuel.mortensonReposting patch from #2972229: Expand the JS test coverage using nightwatch:
👋I'm new to this but made an attempt to add a login, logout, and isLoggedIn command to our Nightwatch instance. Test coverage for the new commands are included.
Edit: We could also have a PHP script that returns an appropriate session cookie based on a user ID, then call that with child_process, if setting a username and password feels weird to people.
Edit 2: Another piece of self-review - the isLoggedIn command accepts a callback because the getCookies method is asynchronous, but the login and logout commands do not accept a callback even though they call isLoggedIn. Should they also take a callback? I researched this a little bit and it looks like with asynchronous _stuff_ in Nightwatch, you have to use callbacks, but can end up nesting many levels deep. Thoughts?
Comment #4
jibranhmmm
Comment #5
GrandmaGlassesRopeManThis failed due to a permissions issue on the test bot,
Comment #6
dawehnerLet's prefix these commands with Drupal :)
Should we prefix this command with "userIsLoggedIn"?
Nitpick: We could have used Array.prototype.some which stops early.
Nice! It does the same as BTB
I'm curious whether we should have some additional assertions here? I guess we are fine actually.
Comment #7
samuel.mortensonAddressed #6. Additional assertions should not be needed, although the test does looks funny without them. :-)
Putting back into needs review, but would like someone else to look at my "Edit 2" comment from #2. I also now wonder if "getCookies" is actually asyncronous, or if it just happens to take a callback.
Comment #8
dawehnerI tried to look at the sourcecode of nightwatch, and well, I couldn't 100% figure that out to be honest. One thing though which confuses me here: Is this helpful at all when callback is not a function? If it isn't this function wouldn't do anything.
Comment #9
samuel.mortenson@dawehner I guess I was just trying to avoid runtime errors, but we could remove the check so users could at least see an error (ex: Uncaught TypeError callback.call is not a function).
Comment #10
webchickAccording to @lauriii this is a blocker for the JS Nightwatch testing stuff.
Comment #11
jibranUser login is useless as it is if we don't allow passing the roles or permissions while creating the user.
Comment #12
dawehnerWhat would be the point of making the callable optional?
Comment #13
jibran@lauriii just pointed me to #2974619: Create nightwatch command to install modules I think creating a console command to create the user with given role/permissions is the way to fix #11.
Comment #14
lauriiiYesterday @Wim Leers mentioned on a call that we could use #2403307: RPC endpoints for user authentication: log in, check login status, log out for logging in, instead of using the login form.
Comment #15
dawehnerNightwatch
@justafish, @alexpott, @lauriii, @drpal had a discussion about nightwatch tests at Drupal dev days in a room without real sunlight.
Goals
Result
we use a browser, like moving to /admin/modules check the checkbox for a module and hit save
Comment #16
jibranI still think that generic console commands i.e. creating a user and installing module would be really helpful but these goals do make sense to me.
We'd need a user 1 for that, right?
You mean nightwatch commands not symfony console commands?
We can always run JS and PHP tests in parallel.
Comment #17
Wim Leers+1, this seems like a great initial direction.
Comment #18
lauriiiCreated a new patch based on #15.
Comment #19
dawehnerGreat progress here!
Should we use utils.promisify here instead?
Let's drop that :)
How is this not longer a problem?
Let's have some assertions here
When the argument is optional we should either have some form of default value, or do some validation
Comment #20
GrandmaGlassesRopeMan➡ #19.1 - this gets a bit confusing, i gave it a shot but couldn't get it working.
➡ #19.2 - fixed
➡ #19.3 - fixed
➡ #19.4 - fixed
➡ #19.5 - php help please.
Comment #21
jibranAdded a comment to explain it.
Comment #22
GrandmaGlassesRopeManThis comment format should be like,
Comment #23
jibranThanks, for the review. I follow the
core/tests/Drupal/Nightwatch/Commands/drupalUninstall.js
comment pattern.Comment #24
dawehnerIf we check for this, we should actually provide a helpful error message: 'The "%s" argument needs to be a number, but it is %s'. Also $name doesn't exist.
Comment #25
lauriiiFixes #24.
Comment #26
dawehnerThank you @lauriii
I'm wondering whether it would be sensible to have some level of test coverage here as well.
Comment #27
jibran> I'm wondering whether it would be sensible to have some level of test coverage here as well.
Going to write a PHP test for the console command.
Comment #28
Wim LeersAFAICT We don't need this. This seems to be copy/pasted from
TestSiteInstallCommand
, which is a special case.Same.
s/id/ID/
s/to/for/
s/which/whom/
Also, why isn't this required?
s/a number/an integer/
Comment #29
Wim LeersThis only does the
bit from the issue title.Either the patch still needs a
command too, or the issue title and summary need to be updated.Comment #30
lauriiiWe don't need PHP command for logging out. There's a Nightwatch command for doing that. 🙌
Comment #31
Wim LeersOk!
Comment #32
Wim LeersAlso.
Comment #33
lauriiiComment #34
jibranFixed #28 and #26.
Comment #35
Wim LeersTalked to @lauriii, I didn't realize there were both Nightwatch and Drupal commands (because I was asked to review the PHP side). I think this issue title covers it all.
Comment #36
Wim LeersThis is still optional. Why?
Shouldn't we also assert the output?
And in the latter case: shouldn't we perform a HTTP request to the given URL and verify that the response contains a
Set-Cookie
header with the Drupal session cookie?Comment #37
GrandmaGlassesRopeMan- fixes linting and jsdoc issues.
Comment #38
martin107 CreditAttribution: martin107 as a volunteer commented... coming from the meeting in slack... where there was a call for review.
FWIW after a slow visual scan of the patch all the code patterns employed here...look straight out of the text book...
nothing that leaps out as questionable.
My feeling - A good addition to the project
So +1 from me.
Comment #39
lauriiiAddresses #36.
Comment #40
jibranFWIW, this assert is checking the change in install command.
Comment #41
Wim LeersComment #42
jibranWriting a change record.
Comment #43
jibranCreated the following change record https://www.drupal.org/node/2986276. Feel free to improve it.
Comment #45
alexpottCommitted and pushed 0083ba97d7 to 8.7.x and c5782b95a2 to 8.6.x. Thanks!
Comment #48
jibranPublished the change record as well. Thanks! for the commit.
Comment #50
Gábor Hojtsy