Skip to content

Commit

Permalink
added migration function for noRep timeframes #1005
Browse files Browse the repository at this point in the history
  • Loading branch information
hansmorb committed Oct 11, 2023
1 parent f4c35ed commit 6bcd939
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
29 changes: 29 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ public static function runTasksAfterUpdate() {
// Set default values to existing timeframes for advance booking days
self::setAdvanceBookingDaysDefault();

//will migrate old timeframes with no repetitions to new timeframes with daily repetition
self::timeframeNoRepToDaily();

// Clear cache
self::clearCache();

Expand Down Expand Up @@ -868,4 +871,30 @@ public static function setAdvanceBookingDaysDefault() {
}
}

/**
* Will convert all timeframes with the old "norep" setting to use the "daily" setting in the repetition
*
* @since 2.9 (expected)
* @return void
* @throws \Exception
*/
public static function timeframeNoRepToDaily() {
$timeframes = \CommonsBooking\Repository\Timeframe::getBookable( [], [], null, true );
/** @var \CommonsBooking\Model\Timeframe $timeframe */
foreach ( $timeframes as $timeframe ) {
if ($timeframe->getRepetition() === 'norep') {
//you could previously set a timeframe with norep to just have a start-date and it would just be valid for the start-date.
//so we have to give norep timeframes without an end-date the start-date as end-date
if ( empty ( $timeframe->getMeta(\CommonsBooking\Model\Timeframe::REPETITION_END) ) ) {
update_post_meta(
$timeframe->ID,
\CommonsBooking\Model\Timeframe::REPETITION_END,
$timeframe->getMeta(\CommonsBooking\Model\Timeframe::REPETITION_START)
);
}
update_post_meta( $timeframe->ID, \CommonsBooking\Model\Timeframe::META_REPETITION, 'd' );
}
}
}

}
39 changes: 36 additions & 3 deletions tests/php/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace CommonsBooking\Tests;

use CommonsBooking\Exception\PostException;
use CommonsBooking\Model\CustomPost;
use CommonsBooking\Plugin;
use CommonsBooking\Tests\Wordpress\CustomPostTypeTest;
use CommonsBooking\Wordpress\CustomPostType\CustomPostType;
use PHPUnit\Framework\TestCase;

class PluginTest extends TestCase
class PluginTest extends CustomPostTypeTest
{

public function testGetCustomPostTypes()
Expand All @@ -28,4 +27,38 @@ public function testGetCustomPostTypes()
$this->assertInstanceOf(CustomPost::class, CustomPostType::getModel($post));
}
}

public function testTimeframeNoRepToDaily() {
$timeframeNoRepWEnd = new \CommonsBooking\Model\Timeframe(
$this->createTimeframe(
$this->locationId,
$this->itemId,
strtotime( '+1 days', strtotime( self::CURRENT_DATE ) ),
strtotime( '+2 days', strtotime( self::CURRENT_DATE ) ),
\CommonsBooking\Wordpress\CustomPostType\Timeframe::BOOKABLE_ID,
'on',
'norep',
)
);
$timeframeNoRepWithoutEnd = new \CommonsBooking\Model\Timeframe(
$this->createTimeframe(
$this->locationId,
$this->itemId,
strtotime( '+1 days', strtotime( self::CURRENT_DATE ) ),
null,
\CommonsBooking\Wordpress\CustomPostType\Timeframe::BOOKABLE_ID,
'on',
'norep',
)
);
$this->assertFalse($timeframeNoRepWithoutEnd->getEndDate());
Plugin::timeframeNoRepToDaily();
$this->assertEquals('d',$timeframeNoRepWEnd->getRepetition());
$this->assertEquals('d',$timeframeNoRepWithoutEnd->getRepetition());
$this->assertEquals(strtotime( '+1 days', strtotime( self::CURRENT_DATE ) ),$timeframeNoRepWithoutEnd->getEndDate());
}

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

0 comments on commit 6bcd939

Please sign in to comment.