aaded some tests, which are now passing
This commit is contained in:
parent
18f8e6cab8
commit
6e91a54dc4
@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\opencase_defaults\Functional;
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Simple test to ensure that main page loads with module enabled.
|
||||
*
|
||||
* @group opencase_defaults
|
||||
*/
|
||||
class LoadTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['opencase_defaults'];
|
||||
|
||||
/**
|
||||
* A user with permission to administer site configuration.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->user = $this->drupalCreateUser(['administer site configuration']);
|
||||
$this->drupalLogin($this->user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the home page loads with a 200 response.
|
||||
*/
|
||||
public function testLoad() {
|
||||
$this->drupalGet(Url::fromRoute('<front>'));
|
||||
$this->assertSession()->statusCodeEquals(200);
|
||||
}
|
||||
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\opencase_entities\Functional;
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Simple test to ensure that main page loads with module enabled.
|
||||
*
|
||||
* @group opencase_entities
|
||||
*/
|
||||
class LoadTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['opencase_entities'];
|
||||
|
||||
/**
|
||||
* A user with permission to administer site configuration.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->user = $this->drupalCreateUser(['administer site configuration']);
|
||||
$this->drupalLogin($this->user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the home page loads with a 200 response.
|
||||
*/
|
||||
public function testLoad() {
|
||||
$this->drupalGet(Url::fromRoute('<front>'));
|
||||
$this->assertSession()->statusCodeEquals(200);
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\opencase\Functional;
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Simple test to ensure that main page loads with module enabled.
|
||||
*
|
||||
* @group opencase
|
||||
*/
|
||||
class LoadTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['opencase'];
|
||||
|
||||
/**
|
||||
* A user with permission to administer site configuration.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->user = $this->drupalCreateUser(['administer site configuration']);
|
||||
$this->drupalLogin($this->user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the home page loads with a 200 response.
|
||||
*/
|
||||
public function testLoad() {
|
||||
$this->drupalGet(Url::fromRoute('<front>'));
|
||||
$this->assertSession()->statusCodeEquals(200);
|
||||
}
|
||||
|
||||
}
|
35
tests/src/Unit/TimeBasedFieldUpdaterTest.php
Normal file
35
tests/src/Unit/TimeBasedFieldUpdaterTest.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace Drupal\Tests\opencase\Unit;
|
||||
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Drupal\opencase\TimeBasedFieldUpdater;
|
||||
|
||||
class TimeBasedFieldUpdaterTest extends UnitTestCase{
|
||||
|
||||
function setUp():void {
|
||||
$this->entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
|
||||
$this->storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->getMock();
|
||||
$this->query = $this->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')->getMock();
|
||||
$this->entityTypeManager->method('getStorage')->willReturn($this->storage);
|
||||
$this->storage->method('getQuery')->willReturn($this->query);
|
||||
}
|
||||
function testFieldIsUpdatedOnEntityReturnedByQuery():void {
|
||||
$this->query->method('execute')->willReturn([1]);
|
||||
$this->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
|
||||
$this->storage->expects($this->once())->method('load')->with(1)->willReturn($this->entity);
|
||||
$updater = new TimeBasedFieldUpdater($this->entityTypeManager, 'dummy_entity', 'dummy_date_field');
|
||||
$updater->update('3 months', ['dummy_status_field' => 3], ['dummy_status_field' => 4]);
|
||||
$this->assertEquals($this->entity->dummy_status_field, 4);
|
||||
}
|
||||
function testFieldIsUpdatedOnAllEntitiesReturnedByQuery():void {
|
||||
$this->query->method('execute')->willReturn([1, 2]);
|
||||
$this->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
|
||||
$this->entity2 = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
|
||||
$this->storage->method('load')->willReturnMap([[1, $this->entity], [2, $this-> entity2]]);
|
||||
$updater = new TimeBasedFieldUpdater($this->entityTypeManager, 'dummy_entity', 'dummy_date_field');
|
||||
$updater->update('3 months', ['dummy_status_field' => 3], ['dummy_status_field' => 4]);
|
||||
$this->assertEquals($this->entity->dummy_status_field, 4);
|
||||
$this->assertEquals($this->entity2->dummy_status_field, 4);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user