Updated: Comment #N

Problem/Motivation

We have no unit test coverage for the date service.

Proposed resolution

Add unit tests for \Drupal\Core\Datetime\Date

Remaining tasks

N/A

User interface changes

N/A

API changes

N/A

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett’s picture

Status: Active » Needs review
FileSize
9.03 KB

I had to inject the config factory and wrap 2 procedural calls in protected methods, but otherwise it was pretty straightforward.

This gives us 100% line coverage of \Drupal\Core\Datetime\Date.

tim.plunkett’s picture

FileSize
9.64 KB
3.06 KB

In IRC, Paris suggesting specifying the DateTime class via the constructor. Seems reasonable, and removes some extra test code.

tim.plunkett’s picture

+++ b/core/lib/Drupal/Core/Datetime/Date.php
@@ -43,16 +43,37 @@ class Date {
+  protected $dateTimeClass = '\Drupal\Core\Datetime\DrupalDateTime';

Duh. I should remove this default value in the next reroll... Waiting for the bot though.

dawehner’s picture

Did you tried to use the static method using the trick described on http://sebastian-bergmann.de/archives/883-Stubbing-and-Mocking-Static-Me... ?

tim.plunkett’s picture

The $dateTimeClass change is not just for testing purposes, it is an actual improvement that will be helpful for anyone swapping out the date service.
But thanks for that link, I will keep that in mind.

And because @dawehner and ParisLiakos both asked, the DrupalDateTime::createFromTimestamp() is a static factory method, so I cannot inject an object.
And in addition, DrupalDateTime extends PHP's \DateTime, so I don't think we should modify its behavior too much to, for example, inject the date service into *it*.

dawehner’s picture

+++ b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
@@ -80,6 +80,15 @@ protected function prepareTimezone($timezone) {
@@ -146,10 +155,10 @@ public function format($format, $settings = array()) {

@@ -146,10 +155,10 @@ public function format($format, $settings = array()) {
+        static::formatDateCallback(NULL, $settings['langcode']);
...
-        $value = preg_replace_callback('/\xEF([AaeDlMTF]?)(.*?)\xFF/', '_format_date_callback', $format);
+        $value = preg_replace_callback('/\xEF([AaeDlMTF]?)(.*?)\xFF/', array(get_class($this), 'formatDateCallback'), $format);

We could make it accept a callable as argument.

ParisLiakos’s picture

Status: Needs review » Needs work
dawehner’s picture

I agree that this was a bit over the top.

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
3.77 KB
11.42 KB

Needed a reroll and some new workarounds after #2098089: Date formats cannot be translated (the core UIs are useless).

ParisLiakos’s picture

sigh:( so much for getting rid of TestDate in the first place..
looks good to me and i ll rtbc once green

dawehner’s picture

  1. +++ b/core/lib/Drupal/Core/Datetime/Date.php
    @@ -185,4 +185,20 @@ protected function country() {
    +    return config_context_enter('Drupal\Core\Config\Context\LanguageConfigContext');
    

    Reading config_context_enter we should just inject the config.context.factory in here and use it

  2. +++ b/core/lib/Drupal/Core/Datetime/Date.php
    @@ -185,4 +185,20 @@ protected function country() {
    +    config_context_leave();
    

    this is directly $this->configFactory->leaveContext()

tim.plunkett’s picture

FileSize
5.49 KB
11.36 KB

That's what I wanted the original issue to do, they said it wasn't doable.

I'm glad it is.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Perfect, way better!

Status: Reviewed & tested by the community » Needs work
Issue tags: -PHPUnit

The last submitted patch, date-2122529-12.patch, failed testing.

tim.plunkett’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: +PHPUnit
FileSize
1.41 KB
11.41 KB

It helps if we actually enter the context.

dawehner’s picture

Does that mean that the unit test does not cover the entering into the config context?

tim.plunkett’s picture

Assigned: tim.plunkett » Unassigned

I have no idea how that would even work.

dawehner’s picture

FileSize
12.4 KB
1.57 KB

There we go.

ParisLiakos’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
martin107’s picture

I am not sure a reroll is appropriate.

Lots has changed since November. This patch was committed. #2111349: Move format_plural to the string translation service and format_interval to the date service.
which notably introduced lots of tests. \Drupal\Tests\Core\Datetime\DateTest is already in core.

Although if people feel strongly the missing goals not covered by the existing tests could be augmented with test from this issue.

Otherwise should this issue be closed?

mikey_p’s picture

I think is still useful, as DateTest only tests formatInterval, and not format, which will be needed when format_date is removed. This should probably move the format_date tests over to DateTest.

mikey_p’s picture

Looks to me like the tests in core/modules/system/src/Tests/Common/FormatDateTest.php should be moved to core/tests/Drupal/Tests/Core/Datetime/DateTest.php.

mikey_p’s picture

In trying to clean this up I ran into a number of issues, and I think we may want to split up some of the behavior of DrupalDateTime. It's currently hard to test the DateFormatter (and format_date()) since it relies on DrupalDateTime::format(), which in turn relies on things like default/per user time zones and the string translation service. Seems like it would make more sense to break it up so that all the formatting work is done in the date formatting service, and DrupalDateTime just relies on that more testable service. Thoughts?

Anonymous’s picture

There's not much to reroll here. We'll need to find out what's left to write tests for.

Re #22: the former is a simpletest whereas the latter is a unit test. Merging those is not really an option I think?

mpdonadio’s picture

Status: Needs work » Postponed

I think we can postpone this until we evaluate the state of things as part of #2543958: [META] DateTime Module Improvements.

mgifford’s picture

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jhedstrom’s picture

Status: Postponed » Postponed (maintainer needs more info)

Not sure what else is to be done here. The class this was opened for has been renamed to DateFormatter, which already has extensive unit test coverage in \Drupal\Tests\Core\Datetime\DateTest.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jhedstrom’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

Closing this out.