Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • project/recurring_events
  • issue/recurring_events-3183502
  • issue/recurring_events-3183463
  • issue/recurring_events-3183483
  • issue/recurring_events-3190526
  • issue/recurring_events-3191715
  • issue/recurring_events-3190833
  • issue/recurring_events-3188808
  • issue/recurring_events-3180479
  • issue/recurring_events-3122823
  • issue/recurring_events-3196649
  • issue/recurring_events-3196428
  • issue/recurring_events-3196702
  • issue/recurring_events-3196704
  • issue/recurring_events-3198532
  • issue/recurring_events-3164409
  • issue/recurring_events-3206960
  • issue/recurring_events-3115678
  • issue/recurring_events-3218496
  • issue/recurring_events-3207435
  • issue/recurring_events-3219082
  • issue/recurring_events-3217367
  • issue/recurring_events-3229514
  • issue/recurring_events-3231841
  • issue/recurring_events-3238591
  • issue/recurring_events-3282502
  • issue/recurring_events-3283128
  • issue/recurring_events-3240862
  • issue/recurring_events-3247034
  • issue/recurring_events-3071679
  • issue/recurring_events-3264621
  • issue/recurring_events-3266436
  • issue/recurring_events-3268690
  • issue/recurring_events-3269555
  • issue/recurring_events-3271328
  • issue/recurring_events-3272361
  • issue/recurring_events-3163804
  • issue/recurring_events-3297681
  • issue/recurring_events-3299575
  • issue/recurring_events-3300786
  • issue/recurring_events-3302916
  • issue/recurring_events-3304286
  • issue/recurring_events-3298679
  • issue/recurring_events-3309652
  • issue/recurring_events-3310360
  • issue/recurring_events-3311843
  • issue/recurring_events-3311712
  • issue/recurring_events-3312003
  • issue/recurring_events-3312084
  • issue/recurring_events-3312242
  • issue/recurring_events-3316080
  • issue/recurring_events-3295367
  • issue/recurring_events-3196417
  • issue/recurring_events-3309859
  • issue/recurring_events-3318590
  • issue/recurring_events-3244975
  • issue/recurring_events-3318998
  • issue/recurring_events-3321269
  • issue/recurring_events-3320512
  • issue/recurring_events-3321235
  • issue/recurring_events-3321550
  • issue/recurring_events-3322998
  • issue/recurring_events-3315836
  • issue/recurring_events-3324055
  • issue/recurring_events-3328907
  • issue/recurring_events-3318490
  • issue/recurring_events-3339288
  • issue/recurring_events-3345618
  • issue/recurring_events-3347935
  • issue/recurring_events-3362297
  • issue/recurring_events-3359696
  • issue/recurring_events-3318666
  • issue/recurring_events-3366907
  • issue/recurring_events-3366910
  • issue/recurring_events-3403064
  • issue/recurring_events-3404311
  • issue/recurring_events-3405567
  • issue/recurring_events-3376639
  • issue/recurring_events-3384836
  • issue/recurring_events-3382387
  • issue/recurring_events-3384389
  • issue/recurring_events-3315503
  • issue/recurring_events-3411229
  • issue/recurring_events-3415222
  • issue/recurring_events-3415308
  • issue/recurring_events-3172514
  • issue/recurring_events-3419694
  • issue/recurring_events-3178696
  • issue/recurring_events-3408924
  • issue/recurring_events-3447130
  • issue/recurring_events-3416436
  • issue/recurring_events-3451613
  • issue/recurring_events-3452632
  • issue/recurring_events-3453086
  • issue/recurring_events-3452641
  • issue/recurring_events-3454012
  • issue/recurring_events-3455716
  • issue/recurring_events-3456300
  • issue/recurring_events-3456641
  • issue/recurring_events-3462327
  • issue/recurring_events-3463467
  • issue/recurring_events-3463979
  • issue/recurring_events-3462480
  • issue/recurring_events-3464792
  • issue/recurring_events-3456045
  • issue/recurring_events-3468300
  • issue/recurring_events-3468521
  • issue/recurring_events-3475611
  • issue/recurring_events-3477247
  • issue/recurring_events-3477047
  • issue/recurring_events-3477650
  • issue/recurring_events-3257502
  • issue/recurring_events-3090186
  • issue/recurring_events-3478802
  • issue/recurring_events-3479449
  • issue/recurring_events-3479843
  • issue/recurring_events-3479860
  • issue/recurring_events-3480495
  • issue/recurring_events-3480500
  • issue/recurring_events-3480746
  • issue/recurring_events-3480973
  • issue/recurring_events-3481021
  • issue/recurring_events-3481722
  • issue/recurring_events-3482804
  • issue/recurring_events-3483283
  • issue/recurring_events-3484209
  • issue/recurring_events-3170156
  • issue/recurring_events-3484558
  • issue/recurring_events-3485904
  • issue/recurring_events-3485935
  • issue/recurring_events-3487412
  • issue/recurring_events-3496270
  • issue/recurring_events-3480508
  • issue/recurring_events-3499792
  • issue/recurring_events-3500920
  • issue/recurring_events-3510919
  • issue/recurring_events-3510942
  • issue/recurring_events-3478268
  • issue/recurring_events-3515976
139 results
Show changes
Showing
with 1502 additions and 14 deletions
<?php
namespace Drupal\recurring_events\Plugin\views\argument;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\taxonomy\Plugin\views\argument\IndexTidDepth;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Argument handler for event series with taxonomy terms with depth.
*
* Normally taxonomy terms with depth contextual filter can be used
* only for content. This handler can be used for Recurring Events series.
*
* Handler expects reference field name, gets reference table and column and
* builds sub query on that table. That is why handler does not need special
* relation table like taxonomy_index.
*
* @ingroup views_argument_handlers
*
* @ViewsArgument("taxonomy_index_tid_eventseries_depth")
*/
class IndexTidEventSeriesDepth extends IndexTidDepth {
/**
* The entity bundle info service.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The database service.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* The entity type.
*
* @var \string
*/
protected $entityType = 'eventseries';
/**
* The entity type label.
*
* @var \string
*/
protected $entityTypeLabel = 'Event Series';
/**
* Construct an instance of the plugin.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $term_storage
* The entity storage interface.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Database\Connection $database
* The database service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $term_storage, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityFieldManagerInterface $entity_field_manager, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $term_storage);
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityFieldManager = $entity_field_manager;
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')->getStorage('taxonomy_term'),
$container->get('entity_type.bundle.info'),
$container->get('entity_field.manager'),
$container->get('database')
);
}
/**
* Extend options.
*/
protected function defineOptions(): array {
$options = parent::defineOptions();
$options['reference_field'] = ['default' => FALSE];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$bundles = array_keys($this->entityTypeBundleInfo->getBundleInfo($this->entityType));
$fields = [];
foreach ($bundles as $bundle) {
$bundle_fields = $this->entityFieldManager->getFieldDefinitions($this->entityType, $bundle);
foreach ($bundle_fields as $name => $field) {
if ($field instanceof FieldConfig) {
if ($field->getType() === 'entity_reference') {
$fields[$name] = $name;
}
}
}
}
$form['reference_field'] = [
'#type' => 'select',
'#title' => $this->t('Reference field'),
'#default_value' => $this->options['reference_field'],
'#description' => $this->t('The Term Reference field name (machine name) on the @type type.', [
'@type' => $this->entityTypeLabel,
]),
'#options' => $fields,
];
parent::buildOptionsForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function query($group_by = FALSE) {
// Get the DB table and reference column name from the reference field name.
$ref_field_name = $this->options['reference_field'] . '_target_id';
$ref_field_table = $this->entityType . '__' . $this->options['reference_field'];
$this->ensureMyTable();
if (!empty($this->options['break_phrase'])) {
$break = static::breakString($this->argument);
if ($break->value === [-1]) {
return FALSE;
}
$operator = (count($break->value) > 1) ? 'IN' : '=';
$tids = $break->value;
}
else {
$operator = "=";
$tids = $this->argument;
}
// Now build the subqueries.
$subquery = $this->database->select($ref_field_table, 'es');
$subquery->addField('es', 'entity_id');
$where = new Condition('OR');
$where->condition('es.' . $ref_field_name, $tids, $operator);
$last = "es";
if ($this->options['depth'] > 0) {
$subquery->leftJoin('taxonomy_term__parent', 'tp', "tp.entity_id = es." . $ref_field_name);
$last = "tp";
foreach (range(1, abs($this->options['depth'])) as $count) {
$subquery->leftJoin('taxonomy_term__parent', "tp$count", "$last.parent_target_id = tp$count.entity_id");
$where->condition("tp$count.entity_id", $tids, $operator);
$last = "tp$count";
}
}
elseif ($this->options['depth'] < 0) {
foreach (range(1, abs($this->options['depth'])) as $count) {
$subquery->leftJoin('taxonomy_term__parent', "tp$count", "$last.entity_id = tp$count.parent_target_id");
$where->condition("tp$count.entity_id", $tids, $operator);
$last = "tp$count";
}
}
$subquery->condition($where);
$this->query->addWhere(0, "$this->tableAlias.$this->realField", $subquery, 'IN');
}
/**
* {@inheritdoc}
*/
public function title() {
$term = $this->termStorage->load($this->argument);
if (!empty($term)) {
$title = $term->getName();
return $title;
}
return $this->t('No name');
}
}
......@@ -2,8 +2,13 @@
namespace Drupal\recurring_events\Plugin\views\field;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Field handler to show the start date of an event series.
......@@ -14,27 +19,121 @@ use Drupal\views\ResultRow;
*/
class EventSeriesStartDate extends FieldPluginBase {
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The current active database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* The views join handler service.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $joinHandler;
/**
* Constructs an EventSeriesStartDate object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Core\Database\Connection $database
* The current active database connection.
* @param \Drupal\Component\Plugin\PluginManagerInterface $join_handler
* The views join handler service.
*/
public function __construct(array $configuration, string $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, Connection $database, PluginManagerInterface $join_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->database = $database;
$this->joinHandler = $join_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
$config_factory = $container->get('config.factory');
/** @var \Drupal\Core\Database\Connection $database */
$database = $container->get('database');
/** @var \Drupal\Component\Plugin\PluginManagerInterface $join_handler */
$join_handler = $container->get('plugin.manager.views.join');
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$config_factory,
$database,
$join_handler
);
}
/**
* {@inheritdoc}
*/
public function query() {
// Leave empty to avoid a query on this field.
// Add a subquery to get the earliest instance start date for a series.
$subQuery = $this->database->select('eventinstance_field_data', 'eventinstance_field_data');
$subQuery->addField('eventinstance_field_data', 'eventseries_id');
$subQuery->addExpression("MIN(eventinstance_field_data.date__value)", 'eventseries_start_date');
$subQuery->groupBy("eventinstance_field_data.eventseries_id");
// Create a join for the subquery.
$joinDefinition = [
'table formula' => $subQuery,
'field' => 'eventseries_id',
'left_table' => 'eventseries_field_data',
'left_field' => 'id',
'adjust' => TRUE,
];
// Add the subquery join to the main query.
/** @var \Drupal\views\Plugin\views\join\JoinPluginBase $join */
$join = $this->joinHandler->createInstance('standard', $joinDefinition);
$this->query->addRelationship('eventseries_start_date', $join, 'eventseries_field_data');
// Add the field to the view.
$this->query->addField(NULL, 'eventseries_start_date', 'eventseries_start_date');
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$start_date = 'N/A';
if (!isset($values->eventseries_start_date)) {
return 'N/A';
}
$event = $values->_entity;
$event_start = $event->getSeriesStart();
$date = new DrupalDateTime($values->eventseries_start_date, 'UTC');
$format = $this->configFactory->get('recurring_events.eventseries.config')->get('date_format');
return $date->format($format, [
'timezone' => date_default_timezone_get(),
]);
}
if (!empty($event_start)) {
$format = \Drupal::config('recurring_events.eventseries.config')->get('date_format');
$start_date = $event_start->format($format);
/**
* {@inheritdoc}
*/
public function clickSort($order) {
if (isset($this->field_alias)) {
$params = $this->options['group_type'] != 'group' ? ['function' => $this->options['group_type']] : [];
$this->query->addOrderBy(NULL, 'eventseries_start_date', $order, $this->field_alias, $params);
}
return $start_date;
}
}
<?php
namespace Drupal\recurring_events\Plugin\views\filter;
/**
* Filter handler for taxonomy terms with depth.
*
* This handler is actually part of the node table and has some restrictions,
* because it uses a subquery to find nodes with.
*
* @ingroup views_filter_handlers
*
* @ViewsFilter("taxonomy_index_tid_eventinstance_depth")
*/
class TaxonomyIndexTidEventInstanceDepth extends TaxonomyIndexTidEventSeriesDepth {
/**
* The entity type.
*
* @var \string
*/
protected $entityType = 'eventinstance';
/**
* The entity type label.
*
* @var \string
*/
protected $entityTypeLabel = 'Event Instance';
}
<?php
namespace Drupal\recurring_events\Plugin\views\filter;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid;
use Drupal\taxonomy\TermStorageInterface;
use Drupal\taxonomy\VocabularyStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Filter handler for taxonomy terms with depth.
*
* This handler is actually part of the node table and has some restrictions,
* because it uses a subquery to find nodes with.
*
* @ingroup views_filter_handlers
*
* @ViewsFilter("taxonomy_index_tid_eventseries_depth")
*/
class TaxonomyIndexTidEventSeriesDepth extends TaxonomyIndexTid {
/**
* The entity bundle info service.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The database service.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* The entity type.
*
* @var \string
*/
protected $entityType = 'eventseries';
/**
* The entity type label.
*
* @var \string
*/
protected $entityTypeLabel = 'Event Series';
/**
* Constructs a TaxonomyIndexTid object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
* The vocabulary storage.
* @param \Drupal\taxonomy\TermStorageInterface $term_storage
* The term storage.
* @param \Drupal\Core\Session\AccountInterface|null $current_user
* The current user.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface|null $entity_type_bundle_info
* The entity type bundle service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface|null $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Database\Connection $database
* The database service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, VocabularyStorageInterface $vocabulary_storage, TermStorageInterface $term_storage, ?AccountInterface $current_user = NULL, ?EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, ?EntityFieldManagerInterface $entity_field_manager = NULL, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $vocabulary_storage, $term_storage, $current_user);
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityFieldManager = $entity_field_manager;
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')->getStorage('taxonomy_vocabulary'),
$container->get('entity_type.manager')->getStorage('taxonomy_term'),
$container->get('current_user'),
$container->get('entity_type.bundle.info'),
$container->get('entity_field.manager'),
$container->get('database')
);
}
/**
* {@inheritDoc}
*/
public function operatorOptions($which = 'title') {
return [
'or' => $this->t('Is one of'),
];
}
/**
* {@inheritDoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['reference_field'] = ['default' => FALSE];
$options['depth'] = ['default' => 0];
return $options;
}
/**
* {@inheritDoc}
*/
public function buildExtraOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildExtraOptionsForm($form, $form_state);
$bundles = array_keys($this->entityTypeBundleInfo->getBundleInfo($this->entityType));
$fields = [];
foreach ($bundles as $bundle) {
$bundle_fields = $this->entityFieldManager->getFieldDefinitions($this->entityType, $bundle);
foreach ($bundle_fields as $name => $field) {
if ($field instanceof FieldConfig) {
if ($field->getType() === 'entity_reference') {
$fields[$name] = $name;
}
}
}
}
$form['reference_field'] = [
'#type' => 'select',
'#title' => $this->t('Reference field'),
'#default_value' => $this->options['reference_field'],
'#description' => $this->t('The field name (machine name) in the product type, which is referencing to a taxonomy. For example field_product_category.'),
'#options' => $fields,
];
$form['depth'] = [
'#type' => 'weight',
'#title' => $this->t('Depth'),
'#default_value' => $this->options['depth'],
'#description' => $this->t('The depth will match nodes tagged with terms in the hierarchy. For example, if you have the term "fruit" and a child term "apple", with a depth of 1 (or higher) then filtering for the term "fruit" will get nodes that are tagged with "apple" as well as "fruit". If negative, the reverse is true; searching for "apple" will also pick up nodes tagged with "fruit" if depth is -1 (or lower).'),
];
}
/**
* Build the query for the filter.
*/
public function query() {
// Get the DB table and reference column name from the reference field name.
$ref_field_name = $this->options['reference_field'] . '_target_id';
$ref_table_name = $this->entityType . '__' . $this->options['reference_field'];
// If no filter values are present, then do nothing.
if (count($this->value) == 0) {
return;
}
elseif (count($this->value) == 1) {
// Sometimes $this->value is an array with a single element so convert it.
if (is_array($this->value)) {
$this->value = current($this->value);
}
$operator = '=';
}
else {
$operator = 'IN';
}
// The normal use of ensureMyTable() here breaks Views.
// So instead we trick the filter into using the alias of the base table.
// @see https://www.drupal.org/node/271833.
// If a relationship is set, we must use the alias it provides.
if (!empty($this->relationship)) {
$this->tableAlias = $this->relationship;
}
// If no relationship, then use the alias of the base table.
else {
$this->tableAlias = $this->query->ensureTable($this->view->storage->get('base_table'));
}
// Now build the subqueries.
$subquery = $this->database->select($ref_table_name, 'tn');
$subquery->addField('tn', 'entity_id');
$where = new Condition('OR');
$where->condition('tn.' . $ref_field_name, $this->value, $operator);
$last = "tn";
if ($this->options['depth'] > 0) {
$subquery->leftJoin('taxonomy_term__parent', 'tp', "tp.entity_id = tn." . $ref_field_name);
$last = "tp";
foreach (range(1, abs($this->options['depth'])) as $count) {
$subquery->leftJoin('taxonomy_term__parent', "tp$count", "$last.parent_target_id = tp$count.entity_id");
$where->condition("tp$count.entity_id", $this->value, $operator);
$last = "tp$count";
}
}
elseif ($this->options['depth'] < 0) {
foreach (range(1, abs($this->options['depth'])) as $count) {
$subquery->leftJoin('taxonomy_term__parent', "tp$count", "$last.entity_id = tp$count.parent_target_id");
$where->condition("tp$count.entity_id", $this->value, $operator);
$last = "tp$count";
}
}
$subquery->condition($where);
$this->query->addWhere($this->options['group'], "$this->tableAlias.$this->realField", $subquery, 'IN');
}
}
......@@ -2,18 +2,18 @@
namespace Drupal\recurring_events;
use Drupal\recurring_events\Entity\EventSeries;
use Drupal\Core\Form\FormStateInterface;
use Drupal\recurring_events\Entity\EventSeries;
/**
* RecurringEventsFieldTypeInterface interface.
* Provides an interface for recurring event type fields.
*/
interface RecurringEventsFieldTypeInterface {
/**
* Converts an EventSeries entity's recurring configuration to an array.
*
* @param Drupal\recurring_events\Entity\EventSeries $event
* @param \Drupal\recurring_events\Entity\EventSeries $event
* The stored event series entity.
*
* @return array
......@@ -24,7 +24,7 @@ interface RecurringEventsFieldTypeInterface {
/**
* Converts a form state object's recurring configuration to an array.
*
* @param Drupal\Core\Form\FormStateInterface $form_state
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state of an updated event series entity.
*
* @return array
......
......@@ -17,7 +17,7 @@ class LoadTest extends BrowserTestBase {
*
* @var array
*/
public static $modules = ['recurring_events'];
protected static $modules = ['recurring_events'];
/**
* A user with permission to administer site configuration.
......@@ -36,7 +36,7 @@ class LoadTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected function setUp() {
protected function setUp(): void {
parent::setUp();
$this->user = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($this->user);
......
<?php
namespace Drupal\Tests\recurring_events\Kernel;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\KernelTests\KernelTestBase;
use Drupal\recurring_events\Plugin\Field\FieldType\ConsecutiveRecurringDate;
/**
* @coversDefaultClass \Drupal\recurring_events\Plugin\Field\FieldType\ConsecutiveRecurringDate
* @group recurring_events
* @requires module field_inheritance
*/
class ConsecutiveRecurringDateTest extends KernelTestBase {
/**
* The modules to load to run the test.
*
* @var array
*/
protected static $modules = [
'datetime',
'datetime_range',
'field_inheritance',
'options',
'recurring_events',
'system',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('eventseries');
$this->installEntitySchema('eventinstance');
$this->installConfig([
'field_inheritance',
'recurring_events',
'datetime',
'system',
]);
}
/**
* Tests ConsecutiveRecurringDate::findDailyDatesBetweenDates().
*/
public function testConvertTimeTo24hourFormat() {
// We want to test for generating all the days between Jan 1st and Jan 7th.
$start_date = new DrupalDateTime('2019-01-01 00:00:00');
$end_date = new DrupalDateTime('2019-01-07 00:00:00');
$expected_dates = $dates = [];
$expected_date_objects = [
new DrupalDateTime('2019-01-01 00:00:00'),
new DrupalDateTime('2019-01-02 00:00:00'),
new DrupalDateTime('2019-01-03 00:00:00'),
new DrupalDateTime('2019-01-04 00:00:00'),
new DrupalDateTime('2019-01-05 00:00:00'),
new DrupalDateTime('2019-01-06 00:00:00'),
new DrupalDateTime('2019-01-07 00:00:00'),
];
$date_objects = ConsecutiveRecurringDate::findDailyDatesBetweenDates($start_date, $end_date);
// Because the objects themselves will be different we convert each of the
// date time objects into an ISO standard date format for comparison.
foreach ($expected_date_objects as $date) {
$expected_dates[] = $date->format('r');
}
foreach ($date_objects as $date) {
$dates[] = $date->format('r');
}
$this->assertEquals($expected_dates, $dates);
}
/**
* Tests ConsecutiveRecurringDate::findSlotsBetweenTimes().
*/
public function testFindSlotsBetweenTimes() {
// We want to test for generating all the time slots between midnight and
// 1am with a 10min duration and 5min buffer.
$start_date = new DrupalDateTime('2019-01-01 00:00:00');
$form_data = [
'end_time' => '01:00:00',
'duration' => '10',
'duration_units' => 'minute',
'buffer' => '5',
'buffer_units' => 'minute',
];
$expected_dates = $dates = [];
$expected_date_objects = [
new DrupalDateTime('2019-01-01 00:00:00'),
new DrupalDateTime('2019-01-01 00:15:00'),
new DrupalDateTime('2019-01-01 00:30:00'),
new DrupalDateTime('2019-01-01 00:45:00'),
new DrupalDateTime('2019-01-01 01:00:00'),
];
$date_objects = ConsecutiveRecurringDate::findSlotsBetweenTimes($start_date, $form_data);
// Because the objects themselves will be different we convert each of the
// date time objects into an ISO standard date format for comparison.
foreach ($expected_date_objects as $date) {
$expected_dates[] = $date->format('r');
}
foreach ($date_objects as $date) {
$dates[] = $date->format('r');
}
$this->assertSame($expected_dates, $dates);
}
}
<?php
namespace Drupal\Tests\recurring_events\Kernel;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\KernelTests\KernelTestBase;
use Drupal\recurring_events\Plugin\Field\FieldType\DailyRecurringDate;
/**
* @coversDefaultClass \Drupal\recurring_events\Plugin\Field\FieldType\DailyRecurringDate
* @group recurring_events
* @requires module field_inheritance
*/
class DailyRecurringDateTest extends KernelTestBase {
/**
* The modules to load to run the test.
*
* @var array
*/
protected static $modules = [
'datetime',
'datetime_range',
'field_inheritance',
'options',
'recurring_events',
'system',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('eventseries');
$this->installEntitySchema('eventinstance');
$this->installConfig([
'field_inheritance',
'recurring_events',
'datetime',
'system',
]);
}
/**
* Tests DailyRecurringDate::findDailyDatesBetweenDates().
*/
public function testFindDailyDatesBetweenDates() {
// We want to test for generating all the weekdays between Jan 1st and 31st.
$start_date = new DrupalDateTime('2019-01-01 00:00:00');
$end_date = new DrupalDateTime('2019-01-07 00:00:00');
$expected_dates = $dates = [];
$expected_date_objects = [
new DrupalDateTime('2019-01-01 00:00:00'),
new DrupalDateTime('2019-01-02 00:00:00'),
new DrupalDateTime('2019-01-03 00:00:00'),
new DrupalDateTime('2019-01-04 00:00:00'),
new DrupalDateTime('2019-01-05 00:00:00'),
new DrupalDateTime('2019-01-06 00:00:00'),
new DrupalDateTime('2019-01-07 00:00:00'),
];
$date_objects = DailyRecurringDate::findDailyDatesBetweenDates($start_date, $end_date);
// Because the objects themselves will be different we convert each of the
// date time objects into an ISO standard date format for comparison.
foreach ($expected_date_objects as $date) {
$expected_dates[] = $date->format('r');
}
foreach ($date_objects as $date) {
$dates[] = $date->format('r');
}
$this->assertEquals($expected_dates, $dates);
}
}
<?php
namespace Drupal\Tests\recurring_events\Kernel;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\KernelTests\KernelTestBase;
use Drupal\recurring_events\Plugin\Field\FieldType\MonthlyRecurringDate;
/**
* @coversDefaultClass \Drupal\recurring_events\Plugin\Field\FieldType\MonthlyRecurringDate
* @group recurring_events
* @requires module field_inheritance
*/
class MonthlyRecurringDateTest extends KernelTestBase {
/**
* The modules to load to run the test.
*
* @var array
*/
protected static $modules = [
'datetime',
'datetime_range',
'field_inheritance',
'options',
'recurring_events',
'system',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('eventseries');
$this->installEntitySchema('eventinstance');
$this->installConfig([
'field_inheritance',
'recurring_events',
'datetime',
'system',
]);
}
/**
* Tests MonthlyRecurringDate::findWeekdayOccurrencesBetweenDates().
*/
public function testFindWeekdayOccurrencesBetweenDates() {
// We want to test for generating all the monthdays in January and February.
$start_date = new DrupalDateTime('2019-01-01 00:00:00');
$end_date = new DrupalDateTime('2019-02-28 00:00:00');
$weekdays = [
'Monday',
'Wednesday',
];
$occurrences = [
'first',
'second',
'third',
'fourth',
'last',
];
$expected_dates = $actual_dates = [];
$expected_date_objects = [
'Monday' => [
'first' => [
new DrupalDateTime('2019-01-07 00:00:00'),
new DrupalDateTime('2019-02-04 00:00:00'),
],
'second' => [
new DrupalDateTime('2019-01-14 00:00:00'),
new DrupalDateTime('2019-02-11 00:00:00'),
],
'third' => [
new DrupalDateTime('2019-01-21 00:00:00'),
new DrupalDateTime('2019-02-18 00:00:00'),
],
'fourth' => [
new DrupalDateTime('2019-01-28 00:00:00'),
new DrupalDateTime('2019-02-25 00:00:00'),
],
'last' => [
new DrupalDateTime('2019-01-28 00:00:00'),
new DrupalDateTime('2019-02-25 00:00:00'),
],
],
'Wednesday' => [
'first' => [
new DrupalDateTime('2019-01-02 00:00:00'),
new DrupalDateTime('2019-02-06 00:00:00'),
],
'second' => [
new DrupalDateTime('2019-01-09 00:00:00'),
new DrupalDateTime('2019-02-13 00:00:00'),
],
'third' => [
new DrupalDateTime('2019-01-16 00:00:00'),
new DrupalDateTime('2019-02-20 00:00:00'),
],
'fourth' => [
new DrupalDateTime('2019-01-23 00:00:00'),
new DrupalDateTime('2019-02-27 00:00:00'),
],
'last' => [
new DrupalDateTime('2019-01-30 00:00:00'),
new DrupalDateTime('2019-02-27 00:00:00'),
],
],
];
foreach ($weekdays as $weekday) {
foreach ($occurrences as $occurrence) {
$date_objects[$weekday][$occurrence] = MonthlyRecurringDate::findWeekdayOccurrencesBetweenDates($occurrence, $weekday, $start_date, $end_date);
}
}
// Because the objects themselves will be different we convert each of the
// date time objects into an ISO standard date format for comparison.
foreach ($expected_date_objects as $weekday => $occurrences) {
foreach ($occurrences as $occurrence => $dates) {
foreach ($dates as $date) {
$expected_dates[$weekday][$occurrence][] = $date->format('r');
}
}
}
foreach ($date_objects as $weekday => $occurrences) {
foreach ($occurrences as $occurrence => $dates) {
foreach ($dates as $date) {
$actual_dates[$weekday][$occurrence][] = $date->format('r');
}
}
}
$this->assertEquals($expected_dates, $actual_dates);
}
/**
* Tests MonthlyRecurringDate::findMonthDaysBetweenDates().
*/
public function testFindMonthDaysBetweenDates() {
// We want to test for generating all the monthdays in January and February.
$start_date = new DrupalDateTime('2019-01-01 00:00:00');
$end_date = new DrupalDateTime('2019-02-28 00:00:00');
$month_days = [
'1',
'29',
'-1',
];
$expected_dates = $actual_dates = [];
$expected_date_objects = [
'1' => [
new DrupalDateTime('2019-01-01 00:00:00'),
new DrupalDateTime('2019-02-01 00:00:00'),
],
'29' => [
new DrupalDateTime('2019-01-29 00:00:00'),
],
'-1' => [
new DrupalDateTime('2019-01-31 00:00:00'),
new DrupalDateTime('2019-02-28 00:00:00'),
],
];
foreach ($month_days as $day) {
$date_objects[$day] = MonthlyRecurringDate::findMonthDaysBetweenDates($day, $start_date, $end_date);
}
// Because the objects themselves will be different we convert each of the
// date time objects into an ISO standard date format for comparison.
foreach ($expected_date_objects as $day => $dates) {
foreach ($dates as $date) {
$expected_dates[$day][] = $date->format('r');
}
}
foreach ($date_objects as $day => $dates) {
foreach ($dates as $date) {
$actual_dates[$day][] = $date->format('r');
}
}
$this->assertEquals($expected_dates, $actual_dates);
}
}
<?php
namespace Drupal\Tests\recurring_events\Kernel;
use Drupal\KernelTests\KernelTestBase;
/**
* @coversDefaultClass \Drupal\recurring_events\Plugin\RecurringEventsFieldTrait
* @group recurring_events
* @requires module field_inheritance
*/
class RecurringEventsFieldTraitTest extends KernelTestBase {
/**
* The modules to load to run the test.
*
* @var array
*/
protected static $modules = [
'datetime',
'datetime_range',
'field_inheritance',
'options',
'recurring_events',
'system',
'text',
'user',
];
/**
* A reflection of \Drupal\recurring_events\Plugin\RecurringEventsFieldTrait.
*
* @var \ReflectionClass
*/
protected $reflection;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('eventseries');
$this->installEntitySchema('eventinstance');
$this->installConfig([
'field_inheritance',
'recurring_events',
'datetime',
'system',
]);
$this->reflection = new \ReflectionClass('\Drupal\recurring_events\Plugin\RecurringEventsFieldTrait');
}
/**
* Tests RecurringEventsFieldTrait::convertTimeTo24hourFormat().
*/
public function testConvertTimeTo24hourFormat() {
$method = new \ReflectionMethod('\Drupal\recurring_events\Plugin\RecurringEventsFieldTrait', 'convertTimeTo24hourFormat');
$method->setAccessible(TRUE);
$times = [
'09:30 am' => ['09', '30', '00'],
'09:30 pm' => ['21', '30', '00'],
'11:15 am' => ['11', '15', '00'],
'11:15 pm' => ['23', '15', '00'],
'12:00 am' => ['00', '00', '00'],
'12:00 pm' => ['12', '00', '00'],
];
foreach ($times as $time => $expected) {
$result = $method->invoke($this->reflection, $time);
$this->assertEquals($expected, $result);
}
}
}
<?php
namespace Drupal\Tests\recurring_events\Kernel;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\KernelTests\KernelTestBase;
use Drupal\recurring_events\Plugin\Field\FieldType\WeeklyRecurringDate;
/**
* @coversDefaultClass \Drupal\recurring_events\Plugin\Field\FieldType\WeeklyRecurringDate
* @group recurring_events
* @requires module field_inheritance
*/
class WeeklyRecurringDateTest extends KernelTestBase {
/**
* The modules to load to run the test.
*
* @var array
*/
protected static $modules = [
'datetime',
'datetime_range',
'field_inheritance',
'options',
'recurring_events',
'system',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('eventseries');
$this->installEntitySchema('eventinstance');
$this->installConfig([
'field_inheritance',
'recurring_events',
'datetime',
'system',
]);
}
/**
* Tests WeeklyRecurringDate::findWeekdaysBetweenDates().
*/
public function testFindWeekdaysBetweenDates() {
// We want to test for generating all the weekdays between Jan 1st and 31st.
$start_date = new DrupalDateTime('2019-01-01 00:00:00');
$end_date = new DrupalDateTime('2019-01-30 00:00:00');
$weekdays = ['Monday', 'Wednesday'];
$expected_dates = $actual_dates = [];
$expected_date_objects = [
'Monday' => [
new DrupalDateTime('2019-01-07 00:00:00'),
new DrupalDateTime('2019-01-14 00:00:00'),
new DrupalDateTime('2019-01-21 00:00:00'),
new DrupalDateTime('2019-01-28 00:00:00'),
],
'Wednesday' => [
new DrupalDateTime('2019-01-02 00:00:00'),
new DrupalDateTime('2019-01-09 00:00:00'),
new DrupalDateTime('2019-01-16 00:00:00'),
new DrupalDateTime('2019-01-23 00:00:00'),
new DrupalDateTime('2019-01-30 00:00:00'),
],
];
foreach ($weekdays as $weekday) {
$date_objects[$weekday] = WeeklyRecurringDate::findWeekdaysBetweenDates($weekday, $start_date, $end_date);
}
// Because the objects themselves will be different we convert each of the
// date time objects into an ISO standard date format for comparison.
foreach ($expected_date_objects as $weekday => $dates) {
foreach ($dates as $date) {
$expected_dates[$weekday][] = $date->format('r');
}
}
foreach ($date_objects as $weekday => $dates) {
foreach ($dates as $date) {
$actual_dates[$weekday][] = $date->format('r');
}
}
$this->assertEquals($expected_dates, $actual_dates);
}
}
<?php
namespace Drupal\Tests\recurring_events\Kernel;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\KernelTests\KernelTestBase;
use Drupal\recurring_events\Plugin\Field\FieldType\YearlyRecurringDate;
/**
* @coversDefaultClass \Drupal\recurring_events\Plugin\Field\FieldType\YearlyRecurringDate
* @group recurring_events
* @requires module field_inheritance
*/
class YearlyRecurringDateTest extends KernelTestBase {
/**
* The modules to load to run the test.
*
* @var array
*/
protected static $modules = [
'datetime',
'datetime_range',
'field_inheritance',
'options',
'recurring_events',
'system',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('eventseries');
$this->installEntitySchema('eventinstance');
$this->installConfig([
'field_inheritance',
'recurring_events',
'datetime',
'system',
]);
}
/**
* Tests YearlyRecurringDate::findYearsBetweenDates().
*/
public function testFindYearsBetweenDates() {
$startDate = new DrupalDateTime('2020-01-01 00:00:00');
$endDate = new DrupalDateTime('2025-01-01 00:00:00');
$interval = 2;
$expectedYears = [2020, 2022, 2024];
$actualYears = YearlyRecurringDate::findYearsBetweenDates($startDate, $endDate, $interval);
$this->assertEquals($expectedYears, $actualYears);
}
}
<?php
namespace Drupal\Tests\recurring_events\Unit\Plugin\migrate\destination;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Tests\migrate\Unit\Plugin\migrate\destination\EntityTestBase;
use Drupal\migrate\Row;
use Drupal\recurring_events\Plugin\migrate\destination\EntityEventSeries;
use Prophecy\Argument;
/**
* @coversDefaultClass \Drupal\recurring_events\Plugin\migrate\destination\EntityEventSeries
* @group recurring_events
*/
class EntityEventSeriesTest extends EntityTestBase {
const SAVED_NEW = 1;
const TEST_ENTITY_ID = 8;
/**
* Mocked field type plugin manager service.
*
* @var \Prophecy\Prophecy\ObjectProphecy|\Drupal\Core\Field\FieldTypePluginManagerInterface
*/
protected $fieldTypeManager;
/**
* Mocked account switcher service.
*
* @var \Prophecy\Prophecy\ObjectProphecy|\Drupal\Core\Session\AccountSwitcherInterface
*/
protected $accountSwitcher;
/**
* {@inheritdoc}
*
* @noinspection PhpParamsInspection
* @noinspection PhpUndefinedMethodInspection
*/
protected function setUp(): void {
parent::setUp();
$this->fieldTypeManager = $this->prophesize(FieldTypePluginManagerInterface::class);
$this->accountSwitcher = $this->prophesize(AccountSwitcherInterface::class);
// Handle calls to methods on mocked services in the parent class.
$this->entityType->getKey('id')->willReturn('id');
$this->entityType->getKey('bundle')->willReturn('type');
// Configure the mocked entity storage to create mocked entities.
$entity = $this->prophesize(ContentEntityInterface::class);
$entity->setSyncing(TRUE)->willReturn($entity->reveal());
$entity->isValidationRequired()->willReturn(FALSE);
$entity->validate()->willReturn([]);
$entity->save()->willReturn(self::SAVED_NEW);
$entity->id()->willReturn(self::TEST_ENTITY_ID);
$entity->enforceIsNew()->willReturn($entity->reveal());
$this->storage->create(Argument::type('array'))->willReturn($entity->reveal());
// Set a mocked container for non-injected services called by the plugin.
$language = $this->prophesize(LanguageInterface::class);
$language->getId()->willReturn('en');
$language_manager = $this->prophesize(LanguageManagerInterface::class);
$language_manager->getCurrentLanguage()->willReturn($language->reveal());
$container = new ContainerBuilder();
$container->set('language_manager', $language_manager->reveal());
\Drupal::setContainer($container);
}
/**
* Helper method to create an EntityEventSeries object with mocked services.
*
* @param array $configuration
* Configuration for the destination plugin.
*
* @return \Drupal\recurring_events\Plugin\migrate\destination\EntityEventSeries
* An EntityEventSeries destination plugin with mocked services.
*
* @noinspection PhpParamsInspection
*/
protected function getEntityEventSeries(array $configuration = []): EntityEventSeries {
return new EntityEventSeries(
$configuration,
'entity:eventseries',
[],
$this->migration->reveal(),
$this->storage->reveal(),
[],
$this->entityFieldManager->reveal(),
$this->fieldTypeManager->reveal(),
$this->accountSwitcher->reveal()
);
}
/**
* Tests importing from a D7 source_date_field.
*
* @covers ::import
*/
public function testImportSourceDateField() {
$plugin = $this->getEntityEventSeries([
'default_bundle' => 'default',
'source_date_field' => 'field_event_datetime',
'source_timezone' => 'America/New_York',
]);
// Import a mocked D7 date field.
$values = [
'field_event_datetime' => [
[
'value' => '2022-07-19T12:00:00',
'value2' => '2022-07-19T13:00:00',
'rrule' => 'RRULE:FREQ=WEEKLY',
],
[
'value' => '2022-08-02T12:00:00',
'value2' => '2022-08-02T13:00:00',
],
],
];
$row = new Row($values);
$row->setDestinationProperty('type', 'default');
$ids = $plugin->import($row);
$this->assertEquals([self::TEST_ENTITY_ID], $ids);
// Assert that it added destination properties for the recurrence pattern.
$destination = $row->getDestination();
$this->assertArrayHasKey('recur_type', $destination);
$this->assertArrayHasKey('weekly_recurring_date', $destination);
}
/**
* Tests importing using the 'recurring_date_field' option.
*
* @covers ::import
*/
public function testImportRecurringDateField() {
$plugin = $this->getEntityEventSeries([
'default_bundle' => 'default',
'recurring_date_field' => 'dates',
]);
// Import custom date data.
$row = new Row([]);
$row->setDestinationProperty('type', 'default');
$row->setDestinationProperty('recur_type', 'custom');
$row->setDestinationProperty('dates', [
[
'value' => '2019-09-05T09:00:00',
'end_value' => '2019-09-05T18:00:00',
],
]);
$ids = $plugin->import($row);
$this->assertEquals([self::TEST_ENTITY_ID], $ids);
// Assert that it added the destination property for the recurrence pattern.
$destination = $row->getDestination();
$this->assertArrayHasKey('custom_date', $destination);
}
/**
* Tests that the import can run without arguments.
*
* @covers ::import
*/
public function testImportNoSourceDateField() {
$plugin = $this->getEntityEventSeries([
'default_bundle' => 'default',
]);
$row = new Row([]);
$row->setDestinationProperty('type', 'default');
$ids = $plugin->import($row);
$this->assertEquals([self::TEST_ENTITY_ID], $ids);
}
}
<?php
namespace Drupal\Tests\recurring_events\Unit\Plugin\migrate\process;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
use Drupal\recurring_events\Plugin\migrate\process\RecurringDate;
use Prophecy\Argument;
/**
* @coversDefaultClass \Drupal\recurring_events\Plugin\migrate\process\RecurringDate
* @group recurring_events
*/
class RecurringDateTest extends MigrateProcessTestCase {
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Mocks for non-injected services used by DrupalDateTime.
$language = $this->prophesize(LanguageInterface::class);
$language->getId()->willReturn('en');
$languageManager = $this->prophesize(LanguageManagerInterface::class);
$languageManager->getCurrentLanguage()->willReturn($language->reveal());
$stringTranslation = $this->prophesize(TranslationInterface::class);
$stringTranslation->translateString(Argument::type(TranslatableMarkup::class))->will(function ($args) {
return $args[0]->getUntranslatedString();
});
// Mocked service container.
$container = new ContainerBuilder();
$container->set('language_manager', $languageManager->reveal());
$container->set('string_translation', $stringTranslation->reveal());
\Drupal::setContainer($container);
}
/**
* Tests processing simple arrays of datetimes.
*
* @covers ::transform
*
* @throws \Drupal\migrate\MigrateException
*/
public function testTransformSimpleValues() {
$configuration = [
'default_timezone' => 'America/New_York',
];
$this->plugin = new RecurringDate($configuration, 'recurring_date', []);
$source = [
[
'20220719T120000',
'20220719T130000',
],
];
$expected = [
[
'value' => '2022-07-19T16:00:00',
'end_value' => '2022-07-19T17:00:00',
],
];
$value = $this->plugin->transform($source, $this->migrateExecutable, $this->row, 'destination_property');
$this->assertSame($expected, $value);
}
/**
* Tests processing associative arrays with timezones and RRULEs.
*
* @covers ::transform
*
* @throws \Drupal\migrate\MigrateException
*/
public function testTransformComplexValues() {
$configuration = [
'value_key' => 'start',
'end_value_key' => 'end',
'rrule_key' => 'rrule',
'timezone_key' => 'timezone',
'default_timezone' => 'America/New_York',
];
$this->plugin = new RecurringDate($configuration, 'recurring_date', []);
$source = [
[
'start' => '20220719T120000',
'end' => '20220719T140000',
'rrule' => 'FREQ=WEEKLY;UNTIL=20220802T140000',
'timezone' => 'America/Detroit',
],
];
$expected = [
'value' => '2022-07-19T16:00:00',
'end_value' => '2022-08-02T18:00:00',
'time' => '04:00 pm',
'end_time' => '06:00 pm',
'duration' => 7200,
'duration_or_end_time' => 'end_time',
'days' => 'tuesday',
];
$value = $this->plugin->transform($source, $this->migrateExecutable, $this->row, 'destination_property');
$this->assertSame($expected, $value);
}
}