View source  
  <?php
namespace Drupal\Tests\datetime\Kernel\Views;
use Drupal\node\Entity\Node;
use Drupal\views\Views;
class ArgumentDateTimeTest extends DateTimeHandlerTestBase {
  
  public static $testViews = [
    'test_argument_datetime',
  ];
  
  protected function setUp($import_test_views = TRUE) : void {
    parent::setUp($import_test_views);
    
    $dates = [
      '2000-10-10',
      '2001-10-10',
      '2002-01-01',
      
      '2002-12-31T23:00:00',
    ];
    foreach ($dates as $date) {
      $node = Node::create([
        'title' => $this
          ->randomMachineName(8),
        'type' => 'page',
        'field_date' => [
          'value' => $date,
        ],
      ]);
      $node
        ->save();
      $this->nodes[] = $node;
    }
  }
  
  public function testDatetimeArgumentYear() {
    $view = Views::getView('test_argument_datetime');
    
    $view
      ->setDisplay('default');
    $this
      ->executeView($view, [
      '2000',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[0]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
    $view
      ->setDisplay('default');
    $this
      ->executeView($view, [
      '2002',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[2]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
    $view
      ->setDisplay('default');
    $this
      ->executeView($view, [
      '2003',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[3]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
    
    $this
      ->setSiteTimezone('America/Vancouver');
    $view
      ->setDisplay('default');
    $this
      ->executeView($view, [
      '2002',
    ]);
    $expected = [];
    
    $expected[] = [
      'nid' => $this->nodes[3]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
  }
  
  public function testDatetimeArgumentMonth() {
    $view = Views::getView('test_argument_datetime');
    
    $view
      ->setDisplay('embed_1');
    $this
      ->executeView($view, [
      '10',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[0]
        ->id(),
    ];
    $expected[] = [
      'nid' => $this->nodes[1]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
    $view
      ->setDisplay('embed_1');
    $this
      ->executeView($view, [
      '01',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[2]
        ->id(),
    ];
    $expected[] = [
      'nid' => $this->nodes[3]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
  }
  
  public function testDatetimeArgumentDay() {
    $view = Views::getView('test_argument_datetime');
    
    $view
      ->setDisplay('embed_2');
    $this
      ->executeView($view, [
      '10',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[0]
        ->id(),
    ];
    $expected[] = [
      'nid' => $this->nodes[1]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
    $view
      ->setDisplay('embed_2');
    $this
      ->executeView($view, [
      '01',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[2]
        ->id(),
    ];
    $expected[] = [
      'nid' => $this->nodes[3]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
  }
  
  public function testDatetimeArgumentAll() {
    $view = Views::getView('test_argument_datetime');
    
    $view
      ->setDisplay('embed_3');
    $this
      ->executeView($view, [
      '2000',
      '10',
      '10',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[0]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
    $view
      ->setDisplay('embed_3');
    $this
      ->executeView($view, [
      '2002',
      '01',
      '01',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[2]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
  }
  
  public function testDatetimeArgumentWeek() {
    $view = Views::getView('test_argument_datetime');
    
    $view
      ->setDisplay('embed_4');
    $this
      ->executeView($view, [
      '41',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[0]
        ->id(),
    ];
    $expected[] = [
      'nid' => $this->nodes[1]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
    $view
      ->setDisplay('embed_4');
    $this
      ->executeView($view, [
      '01',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[2]
        ->id(),
    ];
    $expected[] = [
      'nid' => $this->nodes[3]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
  }
  
  public function testDatetimeArgumentFullDate() {
    $view = Views::getView('test_argument_datetime');
    
    $view
      ->setDisplay('embed_5');
    $this
      ->executeView($view, [
      '20001010',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[0]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
    $view
      ->setDisplay('embed_5');
    $this
      ->executeView($view, [
      '20020101',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[2]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
  }
  
  public function testDatetimeArgumentYearMonth() {
    $view = Views::getView('test_argument_datetime');
    
    $view
      ->setDisplay('embed_6');
    $this
      ->executeView($view, [
      '200010',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[0]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
    $view
      ->setDisplay('embed_6');
    $this
      ->executeView($view, [
      '200201',
    ]);
    $expected = [];
    $expected[] = [
      'nid' => $this->nodes[2]
        ->id(),
    ];
    $this
      ->assertIdenticalResultset($view, $expected, $this->map);
    $view
      ->destroy();
  }
}