Skip to content

Commit

Permalink
[BUGFIX] Article publish date is always replaced by current timestamp (
Browse files Browse the repository at this point in the history
…#283)

Co-authored-by: Benjamin Kott <benjamin.kott@outlook.com>
  • Loading branch information
LeoniePhiline and benjaminkott authored Jul 20, 2023
1 parent 58a1588 commit 965792c
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Classes/Hooks/DataHandlerHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public function processDatamap_afterDatabaseOperations(string $status, string $t
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($table);
$queryBuilder->getRestrictions()->removeAll();
$record = $queryBuilder
->select('*')
$publishDate = $queryBuilder
->select('publish_date')
->from($table)
->where($queryBuilder->expr()->eq('uid', (int)$id))
->executeQuery()
->fetchOne();
if (isset($record)) {
$timestamp = (int) (($record['publish_date'] ?? 0) !== 0 ? $record['publish_date'] : time());
if ($publishDate !== false) {
$timestamp = (int) ($publishDate !== 0 ? $publishDate : time());
$queryBuilder
->update($table)
->set('publish_date', $timestamp)
Expand Down
135 changes: 135 additions & 0 deletions Tests/Functional/Hooks/DataHandlerHookTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package t3g/blog.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace T3G\AgencyPack\Blog\Tests\Functional\Service;

use T3G\AgencyPack\Blog\Constants;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

final class DataHandlerHookTest extends FunctionalTestCase
{
protected array $testExtensionsToLoad = [
'typo3conf/ext/blog'
];

protected function setUp(): void
{
parent::setUp();

$this->importCSVDataSet(__DIR__ . '/../Fixtures/DataHandler/be_users.csv');
$this->setUpBackendUser(1);
Bootstrap::initializeLanguageObject();
}

/**
* @test
*/
public function create(): void
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/BlogBasePages.csv');

$data = [
'pages' => [
'NEW_blogPost1' => [
'pid' => 2,
'hidden' => 0,
'title' => 'Post 1',
'doktype' => Constants::DOKTYPE_BLOG_POST,
'publish_date' => 1689811200,
'crdate_month' => 0,
'crdate_year' => 0,
],
'NEW_blogPage1' => [
'pid' => 1,
'hidden' => 0,
'title' => 'Page 1',
'doktype' => Constants::DOKTYPE_BLOG_PAGE,
'publish_date' => 1689811200,
'crdate_month' => 0,
'crdate_year' => 0,
]
]
];

$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
$dataHandler->start($data, []);
$dataHandler->process_datamap();

/** @var array $post */
$post = BackendUtility::getRecord('pages', 3);
self::assertEquals($post['title'], 'Post 1');
self::assertEquals($post['doktype'], Constants::DOKTYPE_BLOG_POST);
self::assertEquals($post['crdate_month'], 7);
self::assertEquals($post['crdate_year'], 2023);

/** @var array $page */
$page = BackendUtility::getRecord('pages', 4);
self::assertEquals($page['title'], 'Page 1');
self::assertEquals($page['doktype'], Constants::DOKTYPE_BLOG_PAGE);
self::assertEquals($page['crdate_month'], 7);
self::assertEquals($page['crdate_year'], 2023);
}

/**
* @test
*/
public function update(): void
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/BlogBasePages.csv');
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);

// Initial
$initial = [
'pages' => [
'NEW_blogPost1' => [
'pid' => 2,
'hidden' => 0,
'title' => 'Post 1',
'doktype' => Constants::DOKTYPE_BLOG_POST,
'publish_date' => 1689811200,
'crdate_month' => 0,
'crdate_year' => 0,
],
]
];
$dataHandler->start($initial, []);
$dataHandler->process_datamap();

/** @var array $initialRecord */
$initialRecord = BackendUtility::getRecord('pages', 3);
self::assertEquals($initialRecord['title'], 'Post 1');
self::assertEquals($initialRecord['doktype'], Constants::DOKTYPE_BLOG_POST);
self::assertEquals($initialRecord['crdate_month'], 7);
self::assertEquals($initialRecord['crdate_year'], 2023);

// Update
$update = [
'pages' => [
3 => [
'publish_date' => 1653004800,
],
]
];
$dataHandler->start($update, []);
$dataHandler->process_datamap();

/** @var array $updateRecord */
$updateRecord = BackendUtility::getRecord('pages', 3);
self::assertEquals($updateRecord['title'], 'Post 1');
self::assertEquals($updateRecord['doktype'], Constants::DOKTYPE_BLOG_POST);
self::assertEquals($updateRecord['crdate_month'], 5);
self::assertEquals($updateRecord['crdate_year'], 2022);
}
}
9 changes: 9 additions & 0 deletions Tests/Functional/Hooks/Fixtures/BlogBasePages.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"pages",,,,,,,,
,uid,pid,crdate,doktype,title,is_siteroot,module,TSconfig
,1,0,1683627000,138,Blog,1,,"TCEFORM.pages.tags.PAGE_TSCONFIG_ID = 2
TCEFORM.pages.authors.PAGE_TSCONFIG_ID = 2
TCEFORM.pages.categories.PAGE_TSCONFIG_ID = 2
TCEFORM.tt_content.pi_flexform.blog_demandedposts.sDEF.settings\.demand\.authors.PAGE_TSCONFIG_ID = 2
TCEFORM.tt_content.pi_flexform.blog_demandedposts.sDEF.settings\.demand\.tags.PAGE_TSCONFIG_ID = 2
TCEFORM.tt_content.pi_flexform.blog_demandedposts.sDEF.settings\.demand\.categories.PAGE_TSCONFIG_ID = 2"
,2,1,1683627001,254,Data,0,blog,

0 comments on commit 965792c

Please sign in to comment.