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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

longwave’s picture

+  /**
+   * Tests the flook() method.
+   */
+  public function testFlood() {

flook => flood

damiankloip’s picture

FileSize
964 bytes
8.94 KB

hehe, Good spot. Damn, I really want a flook() method....

dawehner’s picture

Mh where is the other issue which had also a unit test for Drupal, posted probably yesterday.

dawehner’s picture

Status: Needs review » Needs work
  1. +++ b/core/tests/Drupal/Tests/Core/DrupalTest.php
    @@ -0,0 +1,336 @@
    +      ->with($this->equalTo('test_config'));
    ...
    +      ->with($this->equalTo('test_queue'), $this->equalTo(TRUE));
    ...
    +      ->with($this->equalTo('test_collection'));
    ...
    +      ->with($this->equalTo('test_entity'), $this->equalTo('OR'));
    ...
    +      ->with($this->equalTo('test_entity'), $this->equalTo('OR'));
    ...
    +      ->with($this->equalTo('test_route'), $this->equalTo($test_array), $this->equalTo($test_array));
    ...
    +      ->with($this->equalTo('Test title'), $this->equalTo('test_route'), $this->equalTo($test_array), $this->equalTo($test_array));
    ...
    +      ->with($this->equalTo($service_name));
    

    You are so not lazy: ... this could be just ->with($service_name) ...

  2. +++ b/core/tests/Drupal/Tests/Core/DrupalTest.php
    @@ -0,0 +1,336 @@
    +    $test_array = array('test' => 'test');
    ...
    +    \Drupal::url('test_route', $test_array, $test_array);
    ...
    +    $test_array = array('test' => 'test');
    ...
    +    \Drupal::l('Test title', 'test_route', $test_array, $test_array);
    

    Any reasons to not have two different arrays and better namings

damiankloip’s picture

Status: Needs work » Needs review
FileSize
4.33 KB
8.82 KB

Thank you!

Status: Needs review » Needs work
Issue tags: -PHPUnit

The last submitted patch, 2089787-5.patch, failed testing.

damiankloip’s picture

Status: Needs work » Needs review
Issue tags: +PHPUnit

#5: 2089787-5.patch queued for re-testing.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Perfect, thank you!

Mile23’s picture

I'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.

damiankloip’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
8.68 KB
9.79 KB

Looks 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.

Mile23’s picture

Status: Needs review » Reviewed & tested by the community

Applies 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.

dawehner’s picture

Just wondering whether we should check the interfaces at least in places where we know that an interface exists?

damiankloip’s picture

Meh, 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..

alexpott’s picture

I'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.

alexpott’s picture

Also 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.

dawehner’s picture

The thing is that some of them just had bugs, which hopefully would be caught in some client test code, but you never know.

Mile23’s picture

FileSize
2.26 KB
9.88 KB

Here's why you want a unit test of \Drupal:

  /**
   * Returns an expirable key value store collection.
   *
   * @param string $collection
   *   The name of the collection holding key and value pairs.
   *
   * @return \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
   *   An expirable key value store collection.
   */
  public static function keyValueExpirable($collection) {
    return static::$container->get('keyvalue.expirable')->get($collection);
  }

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.

Mile23’s picture

Status: Reviewed & tested by the community » Needs review
Mile23’s picture

17: 2089787-17.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 17: 2089787-17.patch, failed testing.

Mile23’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
651 bytes
9.91 KB

Changed typedData() test to typedDataManager().

dawehner’s picture

Can we just add a testcase for formBuilder to complete it? Otherwise this is RTBC

Mile23’s picture

FileSize
615 bytes
10.1 KB

Oo. Lookit that, someone added another method.

Done. :-)

jibran’s picture

Status: Needs review » Reviewed & tested by the community

RTBC as per #22.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

I 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!

Mile23’s picture

FileSize
16.5 KB

...Plus it just looks nice. :-)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.