Skip to content
Snippets Groups Projects
Select Git revision
  • c0a7453aefe71998ca05b3a0d88de216c3f53eb6
  • 11.x default protected
  • 11.2.x protected
  • 10.5.x protected
  • 10.6.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

FormatDateTest.php

  • Nathaniel Catchpole's avatar
    Issue #2339435 by YesCT, alexpott: Default no longer needs to be a property on...
    catch authored
    Issue #2339435 by YesCT, alexpott: Default no longer needs to be a property on Language or ConfigurableLanguage.
    c0a7453a
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    FormatDateTest.php 5.80 KiB
    <?php
    
    /**
     * @file
     * Definition of Drupal\system\Tests\Common\FormatDateTest.
     */
    
    namespace Drupal\system\Tests\Common;
    
    use Drupal\language\Entity\ConfigurableLanguage;
    use Drupal\simpletest\WebTestBase;
    
    /**
     * Tests the format_date() function.
     *
     * @group Common
     */
    class FormatDateTest extends WebTestBase {
    
      /**
       * Modules to enable.
       *
       * @var array
       */
      public static $modules = array('language');
    
      /**
       * Arbitrary langcode for a custom language.
       */
      const LANGCODE = 'xx';
    
      protected function setUp() {
        parent::setUp('language');
    
        \Drupal::config('system.date')
          ->set('timezone.user.configurable', 1)
          ->save();
        $formats = $this->container->get('entity.manager')
          ->getStorage('date_format')
          ->loadMultiple(array('long', 'medium', 'short'));
        $formats['long']->setPattern('l, j. F Y - G:i')->save();
        $formats['medium']->setPattern('j. F Y - G:i')->save();
        $formats['short']->setPattern('Y M j - g:ia')->save();
        $this->refreshVariables();
    
        $this->settingsSet('locale_custom_strings_' . self::LANGCODE, array(
          '' => array('Sunday' => 'domingo'),
          'Long month name' => array('March' => 'marzo'),
        ));
    
        ConfigurableLanguage::createFromLangcode(static::LANGCODE)->save();
        $this->resetAll();
      }
    
      /**
       * Tests admin-defined formats in format_date().
       */
      function testAdminDefinedFormatDate() {
        // Create an admin user.
        $this->admin_user = $this->drupalCreateUser(array('administer site configuration'));
        $this->drupalLogin($this->admin_user);
    
        // Add new date format.
        $edit = array(
          'id' => 'example_style',
          'label' => 'Example Style',
          'date_format_pattern' => 'j M y',
        );
        $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
    
        // Add a second date format with a different case than the first.
        $edit = array(
          'id' => 'example_style_uppercase',
          'label' => 'Example Style Uppercase',
          'date_format_pattern' => 'j M Y',
        );
        $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
        $this->assertText(t('Custom date format added.'));
    
        $timestamp = strtotime('2007-03-10T00:00:00+00:00');
        $this->assertIdentical(format_date($timestamp, 'example_style', '', 'America/Los_Angeles'), '9 Mar 07');
        $this->assertIdentical(format_date($timestamp, 'example_style_uppercase', '', 'America/Los_Angeles'), '9 Mar 2007');
        $this->assertIdentical(format_date($timestamp, 'undefined_style'), format_date($timestamp, 'fallback'), 'Test format_date() defaulting to `fallback` when $type not found.');
      }
    
      /**
       * Tests the format_date() function.
       */
      function testFormatDate() {
        $timestamp = strtotime('2007-03-26T00:00:00+00:00');
        $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', 'en'), 'Sunday, 25-Mar-07 17:00:00 PDT', 'Test all parameters.');
        $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), 'domingo, 25-Mar-07 17:00:00 PDT', 'Test translated format.');
        $this->assertIdentical(format_date($timestamp, 'custom', '\\l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), 'l, 25-Mar-07 17:00:00 PDT', 'Test an escaped format string.');
        $this->assertIdentical(format_date($timestamp, 'custom', '\\\\l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), '\\domingo, 25-Mar-07 17:00:00 PDT', 'Test format containing backslash character.');
        $this->assertIdentical(format_date($timestamp, 'custom', '\\\\\\l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), '\\l, 25-Mar-07 17:00:00 PDT', 'Test format containing backslash followed by escaped format string.');
        $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'Europe/London', 'en'), 'Monday, 26-Mar-07 01:00:00 BST', 'Test a different time zone.');
    
        // Change the default language and timezone.
        \Drupal::config('system.site')->set('langcode', static::LANGCODE)->save();
        date_default_timezone_set('America/Los_Angeles');
    
        $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', 'en'), 'Sunday, 25-Mar-07 17:00:00 PDT', 'Test a different language.');
        $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'Europe/London'), 'Monday, 26-Mar-07 01:00:00 BST', 'Test a different time zone.');
        $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T'), 'domingo, 25-Mar-07 17:00:00 PDT', 'Test custom date format.');
        $this->assertIdentical(format_date($timestamp, 'long'), 'domingo, 25. marzo 2007 - 17:00', 'Test long date format.');
        $this->assertIdentical(format_date($timestamp, 'medium'), '25. marzo 2007 - 17:00', 'Test medium date format.');
        $this->assertIdentical(format_date($timestamp, 'short'), '2007 Mar 25 - 5:00pm', 'Test short date format.');
        $this->assertIdentical(format_date($timestamp), '25. marzo 2007 - 17:00', 'Test default date format.');
        // Test HTML time element formats.
        $this->assertIdentical(format_date($timestamp, 'html_datetime'), '2007-03-25T17:00:00-0700', 'Test html_datetime date format.');
        $this->assertIdentical(format_date($timestamp, 'html_date'), '2007-03-25', 'Test html_date date format.');
        $this->assertIdentical(format_date($timestamp, 'html_time'), '17:00:00', 'Test html_time date format.');
        $this->assertIdentical(format_date($timestamp, 'html_yearless_date'), '03-25', 'Test html_yearless_date date format.');
        $this->assertIdentical(format_date($timestamp, 'html_week'), '2007-W12', 'Test html_week date format.');
        $this->assertIdentical(format_date($timestamp, 'html_month'), '2007-03', 'Test html_month date format.');
        $this->assertIdentical(format_date($timestamp, 'html_year'), '2007', 'Test html_year date format.');
      }
    }