From ce1dee51d44921aac6aabf7a27f1c75aefc8540f Mon Sep 17 00:00:00 2001 From: 3Dgoo Date: Tue, 26 Jun 2018 13:17:47 +0930 Subject: [PATCH 1/4] Fix blog archive widget bug Fixes blog archive widget bug to only get the archive months for the blog linked to in this widget. --- code/widgets/BlogArchiveWidget.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/code/widgets/BlogArchiveWidget.php b/code/widgets/BlogArchiveWidget.php index dc97037..11e50fd 100644 --- a/code/widgets/BlogArchiveWidget.php +++ b/code/widgets/BlogArchiveWidget.php @@ -100,7 +100,8 @@ class BlogArchiveWidget extends Widget $query = SQLSelect::create($fields, '"BlogPost' . $suffix . '"') ->addGroupBy($publishDate) ->addOrderBy('"PublishDate" DESC') - ->addWhere(array('"PublishDate" <= ?' => SS_Datetime::now()->Format('Y-m-d H:i:s'))); + ->addLeftJoin('SiteTree' . $suffix, '"SiteTree' . $suffix . '"."ID" = "BlogPost' . $suffix . '"."ID"') + ->addWhere(array('"PublishDate" <= ?' => SS_Datetime::now()->Format('Y-m-d H:i:s'), 'SiteTree' . $suffix . '.ParentID' => $this->BlogID)); $posts = $query->execute(); $result = new ArrayList(); From 11c99e74c86014879fba90970d07991db5125211 Mon Sep 17 00:00:00 2001 From: 3Dgoo Date: Fri, 29 Jun 2018 21:29:11 +0930 Subject: [PATCH 2/4] Adding tests Adding tests to check that archive widget correctly only shows blog posts from the related blog and not any other blog. --- tests/Widgets/BlogArchiveWidgetTest.yml | 36 ++++++++++++++++++------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/tests/Widgets/BlogArchiveWidgetTest.yml b/tests/Widgets/BlogArchiveWidgetTest.yml index 4126956..d457af1 100644 --- a/tests/Widgets/BlogArchiveWidgetTest.yml +++ b/tests/Widgets/BlogArchiveWidgetTest.yml @@ -1,27 +1,45 @@ Blog: - my-blog: - Title: My Blog + blog-a: + Title: Blog A + blog-b: + Title: Blog B BlogPost: post-a: Title: September Digest PublishDate: 2017-09-01 00:00:00 - ParentID: =>Blog.my-blog + ParentID: =>Blog.blog-a post-b: Title: August is Awesome PublishDate: 2017-08-01 00:00:00 - ParentID: =>Blog.my-blog + ParentID: =>Blog.blog-a post-c: Title: 2015 is so two years ago PublishDate: 2015-05-02 00:01:02 - ParentID: =>Blog.my-blog + ParentID: =>Blog.blog-a + post-d: + Title: Blog post on Blog B + PublishDate: 2016-03-13 21:05:36 + ParentID: =>Blog.blog-b + post-e: + Title: Blog post 2 on Blog B + PublishDate: 2016-06-15 10:00:00 + ParentID: =>Blog.blog-b BlogArchiveWidget: - archive-monthly: + archive-monthly-a: NumberToDisplay: 5 ArchiveType: Monthly - BlogID: =>Blog.my-blog - archive-yearly: + BlogID: =>Blog.blog-a + archive-monthly-b: + NumberToDisplay: 5 + ArchiveType: Monthly + BlogID: =>Blog.blog-b + archive-yearly-a: NumberToDisplay: 5 ArchiveType: Yearly - BlogID: =>Blog.my-blog + BlogID: =>Blog.blog-a + archive-yearly-b: + NumberToDisplay: 5 + ArchiveType: Yearly + BlogID: =>Blog.blog-b From aaf3aacd9e964129e0ee7b7985b1024f8db720c0 Mon Sep 17 00:00:00 2001 From: 3Dgoo Date: Fri, 29 Jun 2018 21:30:38 +0930 Subject: [PATCH 3/4] Adding tests Adding tests to check that archive widget correctly only shows blog posts from the related blog and not any other blog. --- tests/Widgets/BlogArchiveWidgetTest.php | 45 ++++++++++++++++++------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/tests/Widgets/BlogArchiveWidgetTest.php b/tests/Widgets/BlogArchiveWidgetTest.php index 5cdf7e4..b641a61 100644 --- a/tests/Widgets/BlogArchiveWidgetTest.php +++ b/tests/Widgets/BlogArchiveWidgetTest.php @@ -26,16 +26,26 @@ class BlogArchiveWidgetTest extends SapphireTest public function testArchiveMonthlyFromStage() { - $widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly'); - $archive = $widget->getArchive(); + $widgetA = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly-a'); + $archiveA = $widgetA->getArchive(); - $this->assertInstanceOf('SS_List', $archive); - $this->assertCount(3, $archive); + $this->assertInstanceOf('SS_List', $archiveA); + $this->assertCount(3, $archiveA); $this->assertDOSContains(array( array('Title' => 'August 2017'), array('Title' => 'September 2017'), array('Title' => 'May 2015'), - ), $archive); + ), $archiveA); + + $widgetB = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly-b'); + $archiveB = $widgetB->getArchive(); + + $this->assertInstanceOf('SS_List', $archiveB); + $this->assertCount(2, $archiveB); + $this->assertDOSContains(array( + array('Title' => 'March 2016'), + array('Title' => 'June 2016'), + ), $archiveB); } public function testArchiveMonthlyFromLive() @@ -45,7 +55,7 @@ class BlogArchiveWidgetTest extends SapphireTest $this->objFromFixture('BlogPost', 'post-b')->doPublish(); Versioned::reading_stage('Live'); - $widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly'); + $widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly-a'); $archive = $widget->getArchive(); $this->assertCount(1, $archive); @@ -60,15 +70,24 @@ class BlogArchiveWidgetTest extends SapphireTest public function testArchiveYearly() { - $widget = $this->objFromFixture('BlogArchiveWidget', 'archive-yearly'); - $archive = $widget->getArchive(); + $widgetA = $this->objFromFixture('BlogArchiveWidget', 'archive-yearly-a'); + $archiveA = $widgetA->getArchive(); - $this->assertInstanceOf('SS_List', $archive); - $this->assertCount(2, $archive); + $this->assertInstanceOf('SS_List', $archiveA); + $this->assertCount(2, $archiveA); $this->assertDOSContains(array( array('Title' => '2017'), array('Title' => '2015'), - ), $archive); + ), $archiveA); + + $widgetB = $this->objFromFixture('BlogArchiveWidget', 'archive-yearly-b'); + $archiveB = $widgetB->getArchive(); + + $this->assertInstanceOf('SS_List', $archiveB); + $this->assertCount(1, $archiveB); + $this->assertDOSContains(array( + array('Title' => '2016'), + ), $archiveB); } public function testArchiveMonthlyWithNewPostsAdded() @@ -76,7 +95,7 @@ class BlogArchiveWidgetTest extends SapphireTest $original = Versioned::current_stage(); Versioned::reading_stage('Stage'); - $widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly'); + $widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly-a'); $archive = $widget->getArchive(); $this->assertCount(3, $archive, 'Three months are shown in the blog archive list from fixtures'); @@ -84,7 +103,7 @@ class BlogArchiveWidgetTest extends SapphireTest SS_Datetime::set_mock_now('2018-01-01 12:00:00'); $newPost = new BlogPost; - $newPost->ParentID = $this->objFromFixture('Blog', 'my-blog')->ID; + $newPost->ParentID = $this->objFromFixture('Blog', 'blog-a')->ID; $newPost->Title = 'My new blog post'; $newPost->PublishDate = '2018-01-01 08:00:00'; // Same day as the mocked now, but slightly earlier $newPost->write(); From 931bcac0634d230ff6c00d5eabfc2955db45669d Mon Sep 17 00:00:00 2001 From: 3Dgoo Date: Fri, 6 Jul 2018 15:17:38 +0930 Subject: [PATCH 4/4] Adding ansi quote to identifier --- code/widgets/BlogArchiveWidget.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/widgets/BlogArchiveWidget.php b/code/widgets/BlogArchiveWidget.php index 11e50fd..4b8fa92 100644 --- a/code/widgets/BlogArchiveWidget.php +++ b/code/widgets/BlogArchiveWidget.php @@ -101,7 +101,7 @@ class BlogArchiveWidget extends Widget ->addGroupBy($publishDate) ->addOrderBy('"PublishDate" DESC') ->addLeftJoin('SiteTree' . $suffix, '"SiteTree' . $suffix . '"."ID" = "BlogPost' . $suffix . '"."ID"') - ->addWhere(array('"PublishDate" <= ?' => SS_Datetime::now()->Format('Y-m-d H:i:s'), 'SiteTree' . $suffix . '.ParentID' => $this->BlogID)); + ->addWhere(array('"PublishDate" <= ?' => SS_Datetime::now()->Format('Y-m-d H:i:s'), '"SiteTree' . $suffix . '"."ParentID"' => $this->BlogID)); $posts = $query->execute(); $result = new ArrayList();