Something went wrong. Try again.
Date range field for Craft CMS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416<?php
namespace studioespresso\daterange\tests\unit;
use Codeception\Test\Unit;use Craft;use craft\db\Query;use craft\elements\db\EntryQuery;use craft\elements\Entry;use craft\fieldlayoutelements\CustomField;use craft\helpers\StringHelper;use craft\models\EntryType;use craft\models\FieldLayout;use craft\models\FieldLayoutTab;use studioespresso\daterange\behaviors\EntryQueryBehavior;use studioespresso\daterange\fields\DateRangeField;use yii\base\InvalidConfigException;
class EntryQueryBehaviorTest extends Unit{ private EntryQuery $query; private DateRangeField $dateRangeField;
protected function _before() { parent::_before(); $this->query = Entry::find();
// Create a DateRangeField $this->dateRangeField = new DateRangeField([ 'name' => 'Event Dates', 'handle' => 'eventDates', ]); Craft::$app->getFields()->saveField($this->dateRangeField);
// Create an EntryType $type = new EntryType([ 'name' => 'Event', 'handle' => 'event', 'hasTitleField' => true, 'titleFormat' => null, 'uid' => StringHelper::UUID(), ]); Craft::$app->getEntries()->saveEntryType($type); $entryType = Craft::$app->getEntries()->getEntryTypeByHandle('event');
// Create a FieldLayout with the field attached $fieldLayout = new FieldLayout([ 'type' => Entry::class, ]); $tab = new FieldLayoutTab([ 'name' => 'Content', 'sortOrder' => 1, ]); // Attach tab to layout first so it has the back-reference $fieldLayout->setTabs([$tab]); $tab->setElements([new CustomField($this->dateRangeField)]); Craft::$app->getFields()->saveLayout($fieldLayout);
// Assign layout to entry type $entryType->fieldLayoutId = $fieldLayout->id; Craft::$app->getEntries()->saveEntryType($entryType); }
protected function _after() { parent::_after();
$type = Craft::$app->getEntries()->getEntryTypeByHandle('event'); if ($type) { Craft::$app->getEntries()->deleteEntryType($type); }
$field = Craft::$app->getFields()->getFieldByHandle('eventDates'); if ($field) { Craft::$app->getFields()->deleteField($field); } }
private function getBehavior(): EntryQueryBehavior { return $this->query->getBehavior('date-range'); }
private function getSubQueryWhereString(): string { $where = $this->query->subQuery->where; if ($where === null) { return ''; } return print_r($where, true); }
// ---- Existing property-setting tests ----
public function testBehaviorIsAttached() { $this->assertInstanceOf(EntryQueryBehavior::class, $this->getBehavior()); }
public function testIsFutureSetsPropertiesAndReturnsOwner() { $result = $this->query->isFuture('myField', 'myType');
$this->assertSame($this->query, $result); $this->assertEquals('myField', $this->getBehavior()->handle); $this->assertTrue($this->getBehavior()->isFuture); $this->assertEquals('myType', $this->getBehavior()->entryTypeHandle); $this->assertFalse($this->getBehavior()->includeToday); }
public function testIsPastSetsPropertiesAndReturnsOwner() { $result = $this->query->isPast('myField', 'myType');
$this->assertSame($this->query, $result); $this->assertEquals('myField', $this->getBehavior()->handle); $this->assertTrue($this->getBehavior()->isPast); $this->assertEquals('myType', $this->getBehavior()->entryTypeHandle); }
public function testIsNotPastSetsPropertiesAndReturnsOwner() { $result = $this->query->isNotPast('myField', 'myType');
$this->assertSame($this->query, $result); $this->assertEquals('myField', $this->getBehavior()->handle); $this->assertTrue($this->getBehavior()->isNotPast); $this->assertEquals('myType', $this->getBehavior()->entryTypeHandle); }
public function testIsOnGoingSetsPropertiesAndReturnsOwner() { $result = $this->query->isOnGoing('myField', 'myType');
$this->assertSame($this->query, $result); $this->assertEquals('myField', $this->getBehavior()->handle); $this->assertTrue($this->getBehavior()->isOnGoing); $this->assertEquals('myType', $this->getBehavior()->entryTypeHandle); }
public function testStringValueSetsHandle() { $this->query->isFuture('eventDate', 'event');
$this->assertEquals('eventDate', $this->getBehavior()->handle); }
public function testArrayWithTypeHandleParsesCorrectly() { $this->query->isFuture(['eventDate', 'event']);
$this->assertEquals('eventDate', $this->getBehavior()->handle); $this->assertEquals('event', $this->getBehavior()->entryTypeHandle); }
public function testArrayWithIncludeTodayParsesCorrectly() { $this->query->isFuture(['eventDate', true], 'event');
$this->assertEquals('eventDate', $this->getBehavior()->handle); $this->assertTrue($this->getBehavior()->includeToday); }
public function testIncludeTodayDefaultsToFalse() { $this->query->isFuture('myField', 'myType');
$this->assertFalse($this->getBehavior()->includeToday); }
// ---- onAfterPrepare: error handling ----
public function testOnAfterPrepareThrowsWhenEntryTypeHandleMissing() { $behavior = $this->getBehavior(); $behavior->handle = 'eventDates'; $behavior->entryTypeHandle = null;
$this->expectException(InvalidConfigException::class); $this->expectExceptionMessage('entryType not specified'); $behavior->onAfterPrepare(); }
public function testOnAfterPrepareThrowsWhenEntryTypeHandleInvalid() { $behavior = $this->getBehavior(); $behavior->handle = 'eventDates'; $behavior->entryTypeHandle = 'nonExistentType';
$this->expectException(InvalidConfigException::class); $this->expectExceptionMessage('Invalid entryType specified'); $behavior->onAfterPrepare(); }
public function testOnAfterPrepareDoesNothingWhenNoHandleSet() { $behavior = $this->getBehavior(); $behavior->onAfterPrepare();
$this->assertFalse($behavior->field); }
// ---- onAfterPrepare: field retrieval + SQL modification ----
public function testOnAfterPrepareLoadsFieldFromLayout() { $this->query->isFuture('eventDates', 'event'); $this->query->subQuery = new Query();
$this->getBehavior()->onAfterPrepare();
$this->assertInstanceOf(DateRangeField::class, $this->getBehavior()->field); }
public function testIsFutureAddsWhereOnStartColumn() { $this->query->isFuture('eventDates', 'event'); $this->query->subQuery = new Query();
$this->getBehavior()->onAfterPrepare();
$whereStr = $this->getSubQueryWhereString(); $this->assertNotEmpty($whereStr); $this->assertStringContainsString('start', $whereStr); }
public function testIsPastAddsWhereOnEndColumn() { $this->query->isPast('eventDates', 'event'); $this->query->subQuery = new Query();
$this->getBehavior()->onAfterPrepare();
$whereStr = $this->getSubQueryWhereString(); $this->assertNotEmpty($whereStr); $this->assertStringContainsString('end', $whereStr); }
public function testIsNotPastAddsWhereOnEndColumn() { $this->query->isNotPast('eventDates', 'event'); $this->query->subQuery = new Query();
$this->getBehavior()->onAfterPrepare();
$whereStr = $this->getSubQueryWhereString(); $this->assertNotEmpty($whereStr); $this->assertStringContainsString('end', $whereStr); }
public function testIsOnGoingAddsWhereOnBothColumns() { $this->query->isOnGoing('eventDates', 'event'); $this->query->subQuery = new Query();
$this->getBehavior()->onAfterPrepare();
$whereStr = $this->getSubQueryWhereString(); $this->assertNotEmpty($whereStr); $this->assertStringContainsString('start', $whereStr); $this->assertStringContainsString('end', $whereStr); }
public function testFieldNotInLayoutAddsNoWhere() { $this->query->isFuture('nonExistentField', 'event'); $this->query->subQuery = new Query();
$this->getBehavior()->onAfterPrepare();
$this->assertNull($this->getBehavior()->field); $this->assertNull($this->query->subQuery->where); }
// ---- startsAfterDate ----
public function testStartsAfterDateSetsPropertiesAndReturnsOwner() { $result = $this->query->startsAfterDate('2025-06-15', 'myField', 'myType');
$this->assertSame($this->query, $result); $this->assertEquals('myField', $this->getBehavior()->handle); $this->assertEquals('myType', $this->getBehavior()->entryTypeHandle); $this->assertNotNull($this->getBehavior()->startsAfterDate); }
public function testStartsAfterDateAcceptsArrayFormat() { $this->query->startsAfterDate(['2025-06-15', 'myField', 'myType']);
$this->assertEquals('myField', $this->getBehavior()->handle); $this->assertEquals('myType', $this->getBehavior()->entryTypeHandle); }
public function testStartsAfterDateAddsWhereOnStartColumn() { $this->query->startsAfterDate('2025-06-15', 'eventDates', 'event'); $this->query->subQuery = new Query();
$this->getBehavior()->onAfterPrepare();
$whereStr = $this->getSubQueryWhereString(); $this->assertNotEmpty($whereStr); $this->assertStringContainsString('start', $whereStr); }
// ---- endsBeforeDate ----
public function testEndsBeforeDateSetsPropertiesAndReturnsOwner() { $result = $this->query->endsBeforeDate('2025-06-15', 'myField', 'myType');
$this->assertSame($this->query, $result); $this->assertEquals('myField', $this->getBehavior()->handle); $this->assertEquals('myType', $this->getBehavior()->entryTypeHandle); $this->assertNotNull($this->getBehavior()->endsBeforeDate); }
public function testEndsBeforeDateAcceptsArrayFormat() { $this->query->endsBeforeDate(['2025-06-15', 'myField', 'myType']);
$this->assertEquals('myField', $this->getBehavior()->handle); $this->assertEquals('myType', $this->getBehavior()->entryTypeHandle); }
public function testEndsBeforeDateAddsWhereOnEndColumn() { $this->query->endsBeforeDate('2025-06-15', 'eventDates', 'event'); $this->query->subQuery = new Query();
$this->getBehavior()->onAfterPrepare();
$whereStr = $this->getSubQueryWhereString(); $this->assertNotEmpty($whereStr); $this->assertStringContainsString('end', $whereStr); }
// ---- isDuringDate ----
public function testIsDuringDateSetsPropertiesAndReturnsOwner() { $result = $this->query->isDuringDate('2025-06-15', 'myField', 'myType');
$this->assertSame($this->query, $result); $this->assertEquals('myField', $this->getBehavior()->handle); $this->assertEquals('myType', $this->getBehavior()->entryTypeHandle); $this->assertNotNull($this->getBehavior()->isDuringDate); }
public function testIsDuringDateAcceptsArrayFormat() { $this->query->isDuringDate(['2025-06-15', 'myField', 'myType']);
$this->assertEquals('myField', $this->getBehavior()->handle); $this->assertEquals('myType', $this->getBehavior()->entryTypeHandle); }
public function testIsDuringDateAddsWhereOnBothColumns() { $this->query->isDuringDate('2025-06-15', 'eventDates', 'event'); $this->query->subQuery = new Query();
$this->getBehavior()->onAfterPrepare();
$whereStr = $this->getSubQueryWhereString(); $this->assertNotEmpty($whereStr); $this->assertStringContainsString('start', $whereStr); $this->assertStringContainsString('end', $whereStr); }
// ---- isNotDuringDate ----
public function testIsNotDuringDateSetsPropertiesAndReturnsOwner() { $result = $this->query->isNotDuringDate('2025-06-15', 'myField', 'myType');
$this->assertSame($this->query, $result); $this->assertEquals('myField', $this->getBehavior()->handle); $this->assertEquals('myType', $this->getBehavior()->entryTypeHandle); $this->assertNotNull($this->getBehavior()->isNotDuringDate); }
public function testIsNotDuringDateAcceptsArrayFormat() { $this->query->isNotDuringDate(['2025-06-15', 'myField', 'myType']);
$this->assertEquals('myField', $this->getBehavior()->handle); $this->assertEquals('myType', $this->getBehavior()->entryTypeHandle); }
public function testIsNotDuringDateAddsWhereWithOrCondition() { $this->query->isNotDuringDate('2025-06-15', 'eventDates', 'event'); $this->query->subQuery = new Query();
$this->getBehavior()->onAfterPrepare();
$whereStr = $this->getSubQueryWhereString(); $this->assertNotEmpty($whereStr); $this->assertStringContainsString('or', $whereStr); $this->assertStringContainsString('start', $whereStr); $this->assertStringContainsString('end', $whereStr); }
// ---- Date range string parsing ----
public function testIsDuringDateAcceptsRangeString() { $this->query->isDuringDate('2025-06-01 => 2025-06-30', 'myField', 'myType');
$this->assertNotNull($this->getBehavior()->isDuringDate); }}