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
In light of fixes like #2078285: Add short-cut methods to the \Drupal class for generating URLs and links from routes that were not found until after commit, we should unit test the \Drupal class methods to make sure the correct service and params are called.
Proposed resolution
Write some tests.
Remaining tasks
Tests
User interface changes
None
API changes
None
Comment | File | Size | Author |
---|---|---|---|
#26 | green_drupal.png | 16.5 KB | Mile23 |
#23 | 2089787_23.patch | 10.1 KB | Mile23 |
#23 | interdiff.txt | 615 bytes | Mile23 |
#21 | 2089787-21.patch | 9.91 KB | Mile23 |
#21 | interdiff.txt | 651 bytes | Mile23 |
Comments
Comment #1
longwaveflook => flood
Comment #2
damiankloip CreditAttribution: damiankloip commentedhehe, Good spot. Damn, I really want a flook() method....
Comment #3
dawehnerMh where is the other issue which had also a unit test for Drupal, posted probably yesterday.
Comment #4
dawehnerYou are so not lazy: ... this could be just ->with($service_name) ...
Any reasons to not have two different arrays and better namings
Comment #5
damiankloip CreditAttribution: damiankloip commentedThank you!
Comment #7
damiankloip CreditAttribution: damiankloip commented#5: 2089787-5.patch queued for re-testing.
Comment #8
dawehnerPerfect, thank you!
Comment #9
Mile23I'd suggest we'll eventually want strict in phpunit.xml.dist, so we should throw explicit assertions, even just assertNotNull().
May be one for the coding standards: #2057905: [policy, no patch] Discuss the standards for phpunit based tests
There are a bunch of other tests with no assertions, so whatever problem this might be is already present and we're better off with the not-strict test than without, so don't let me kill the RTBC.
Comment #10
damiankloip CreditAttribution: damiankloip commentedLooks like this is happening now in #2096595: Make all PHPUnit tests pass with --strict, for strict at least. I would still live to talk about enforcing other standards/settings for PHPUnit though.
So this adds assertNotNull assertions for all the test methods.
Comment #11
Mile23Applies and passes here.
I was concerned that testUrl() and testL() might need dataproviders, but I see that we're testing the round trip through \Drupal, not the services themselves.
Comment #12
dawehnerJust wondering whether we should check the interfaces at least in places where we know that an interface exists?
Comment #13
damiankloip CreditAttribution: damiankloip commentedMeh, the way I see it, we really only care that a service with x name is called. We don't care what is actually returned or anything..
Comment #14
alexpottI've got to admit I do not get the point of this test. The point of a unit test is to test behaviour - if there is no behaviour in the method then why test - see http://stackoverflow.com/questions/6197370/should-unit-tests-be-written-...
Leaving at rtbc for more opinions.
Comment #15
alexpottAlso the issue with #2078285: Add short-cut methods to the \Drupal class for generating URLs and links from routes is that we should not add methods with uses.
Comment #16
dawehnerThe thing is that some of them just had bugs, which hopefully would be caught in some client test code, but you never know.
Comment #17
Mile23Here's why you want a unit test of \Drupal:
Add a typo so it says
return static::$container->get('key_value.expirable')->get($collection);
and run PHPUnit.(Hint: It passes, because there are no unit tests.)
Much more convenient to run an 8-second test than all of SimpleTest to discover this typo.
Here's a version that mocks interfaces instead of implementations and has more specific demands on return values, for testL() and testUrl(). Other classes used don't implement appropriate interfaces, so we stick with the implementation class for those and just leave assertNotNull.
Comment #18
Mile23Comment #19
Mile2317: 2089787-17.patch queued for re-testing.
Comment #21
Mile23Changed typedData() test to typedDataManager().
Comment #22
dawehnerCan we just add a testcase for formBuilder to complete it? Otherwise this is RTBC
Comment #23
Mile23Oo. Lookit that, someone added another method.
Done. :-)
Comment #24
jibranRTBC as per #22.
Comment #25
webchickI actually find the argument in #17 pretty persuasive in why a simple check here makes sense. I'd rather not do these kinds of smokescreen tests to all classes, but the Drupal class is central enough that it'd be nice to pick up issues introduced in it sooner than after commit.
So... Committed and pushed to 8.x. Thanks!
Comment #26
Mile23...Plus it just looks nice. :-)