From f30b37a895c2719b318a5adffb43f87a146aa7f4 Mon Sep 17 00:00:00 2001
From: l26yan <l26yan@uwaterloo.ca>
Date: Tue, 6 Jul 2021 11:34:49 -0400
Subject: [PATCH] ISTWCMS-4918-4919-4920 Test sort by event date (asc) for
 events or date fields (asc) for blog and news

---
 tests/src/Functional/UwWcmsBasicTest.php | 66 +++++++++++++++++++++++-
 1 file changed, 65 insertions(+), 1 deletion(-)

diff --git a/tests/src/Functional/UwWcmsBasicTest.php b/tests/src/Functional/UwWcmsBasicTest.php
index 6f64e5b7f..c58783ed5 100644
--- a/tests/src/Functional/UwWcmsBasicTest.php
+++ b/tests/src/Functional/UwWcmsBasicTest.php
@@ -361,8 +361,72 @@ class UwWcmsBasicTest extends BrowserTestBase {
    * Test content creation.
    */
   private function createContent() {
-    $this->drupalLogin($this->drupalUsers['uw_role_content_author']);
+    $this->drupalLogin($this->drupalUsers['administrator']);
+    // Create three events node including the below dates.
+    $dates = [
+      '2021-07-01',
+      '2021-06-01',
+      '2021-08-01',
+    ];
+    foreach ($dates as $date) {
+      $edits['event'][] = [
+        'edit-title-0-value' => $date,
+        'edit-field-uw-event-summary-0-value' => 'string',
+        'edit-field-uw-event-date-0-value-date' => $date,
+        'edit-field-uw-event-date-0-value-time' => '13:00:00',
+        'edit-field-uw-event-date-0-end-value-date' => $date,
+        'edit-field-uw-event-date-0-end-value-time' => '14:00:00',
+        'edit-field-uw-meta-description-0-value' => 'string',
+        'edit-moderation-state-0-state' => 'published',
+      ];
+      $edits['blog'][] = [
+        'edit-title-0-value' => $date,
+        'edit-field-uw-blog-date-0-value-date' => $date,
+        'edit-field-uw-blog-summary-0-value' => 'string',
+        'edit-field-uw-meta-description-0-value' => 'string',
+        'edit-moderation-state-0-state' => 'published',
+      ];
+      $edits['news_item'][] = [
+        'edit-title-0-value' => $date,
+        'edit-field-uw-news-date-0-value-date' => $date,
+        'edit-field-uw-news-summary-0-value' => 'string',
+        'edit-field-uw-meta-description-0-value' => 'string',
+        'edit-moderation-state-0-state' => 'published',
+      ];
+    }
+    foreach ($edits as $content_type => $items) {
+      foreach ($items as $item) {
+        $this->drupalPostForm('node/add/uw_ct_' . $content_type, $item, 'Save');
+      }
+    }
+
+    // Get expected sort dates for late use.
+    sort($dates);
 
+    $page_views = [
+      'events',
+      'blog',
+      'news',
+    ];
+
+    foreach ($page_views as $view) {
+      $this->drupalGet($view);
+      $this->assertSession()->statusCodeEquals(200);
+      $rows = $this->xpath('//div[@class="views-row"]');
+      // Events: getText() shows "Thursday, July 1, 2021
+      // 1:00 PM - 2:00 PM AEST 2021-07-01 string".
+      // Blog: getText() shows "Tuesday, July 1, 2021 2021-06-01
+      // by administrator string"
+      // News: getTest() shows "Thursday, July 1, 2021
+      // 2021-07-01 string".
+      // Get the actual date from the text above.
+      foreach ($rows as $key => $row) {
+        preg_match("/\d{4}\-\d{2}-\d{2}/", $row->getText(), $matches);
+        $this->assertEqual($matches[0], $dates[$key]);
+      }
+    }
+
+    $this->drupalLogin($this->drupalUsers['uw_role_content_author']);
     // Make sure audience taxonomy to blog, catalog,
     // event and news item content types.
     $types = [
-- 
GitLab