aaded some tests, which are now passing

This commit is contained in:
2022-05-15 16:13:53 +01:00
parent 18f8e6cab8
commit 6e91a54dc4
5 changed files with 45 additions and 144 deletions

View File

@ -2,23 +2,27 @@
namespace Drupal\opencase;
use Drupal;
use Drupal\Core\Entity\EntityTypeManagerInterface;
final class TimeBasedFieldUpdater {
private EntityTypeManagerInterface $entityTypeManager;
private string $date_field;
private string $entity_type;
private array $conditions;
private string $date_format;
final public function __construct($entity_type, $date_field, $conditions = [], $date_format = 'Y-m-d')
{
final public function __construct(EntityTypeManagerInterface $entityTypeManager, string $entity_type, string $date_field, array $conditions = [], string $date_format = 'Y-m-d')
{
$this->entityTypeManager = $entityTypeManager;
$this->date_field = $date_field;
$this->conditions = $conditions;
$this->date_format = $date_format;
$this->entity_type = $entity_type;
}
final public function update($time_elapsed, $old_values, $new_values): void {
$query = Drupal::entityQuery($this->entity_type);
final public function update(string $time_elapsed, array $old_values, array $new_values): void {
$query = $this->entityTypeManager->getStorage($this->entity_type)->getQuery();
foreach($this->conditions as $cond_field=>$cond_value) {
$query->condition($cond_field, $cond_value);
}
@ -27,9 +31,9 @@ final class TimeBasedFieldUpdater {
}
$query->condition($this->date_field, date($this->date_format, strtotime('-'.$time_elapsed)), "<");
foreach($query->execute() as $id) {
$entity = Drupal::entityTypeManager()->getStorage($this->entity_type)->load($id);
$entity = $this->entityTypeManager->getStorage($this->entity_type)->load($id);
foreach($new_values as $new_field=>$new_value) {
$entity->set($new_field, $new_value);
$entity->$new_field = $new_value;
}
$entity->save();
}