Skip to content
Snippets Groups Projects
Commit 0af28012 authored by Jakub Piasecki's avatar Jakub Piasecki Committed by Eirik Morland
Browse files

Issue #3325493 by zaporylie, eiriksm: Get entity query by offer

parent 7c507fef
No related branches found
No related tags found
1 merge request!1Issue #3325493: Get entity query by offer
{
"name": "drupal/affected_by_promotion"
"name": "drupal/affected_by_promotion",
"type": "drupal-module",
"license": "GPL-2.0-or-later",
"require": {
"drupal/commerce": "^2.0"
}
}
......@@ -3,6 +3,7 @@
namespace Drupal\affected_by_promotion;
use Drupal\commerce_promotion\Entity\PromotionInterface;
use Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\PromotionOfferInterface;
/**
* AffectedEntitiesManager service.
......@@ -28,4 +29,22 @@ class AffectedEntitiesManager {
return $offer->getAffectedEntitiesQuery($entity_type_id);
}
/**
* Get the query for finding the affected entities for an offer.
*
* @param \Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\PromotionOfferInterface $offer
* The offer to use for the query.
* @param string $entity_type_id
* The entity type id. For example commerce_product.
*
* @return bool|\Drupal\Core\Database\Query\Query
* Return a query if the offer type supports it, FALSE otherwise.
*/
public function getAffectedEntitiesQueryByOffer(PromotionOfferInterface $offer, $entity_type_id) {
if (!$offer instanceof SupportsAffectedEntitiesQueryInterface) {
return FALSE;
}
return $offer->getAffectedEntitiesQuery($entity_type_id);
}
}
......@@ -101,4 +101,11 @@ abstract class DummyPromotionBase implements PromotionOfferInterface {
// TODO: Implement apply() method.
}
/**
* {@inheritdoc}
*/
public function clear(EntityInterface $entity, PromotionInterface $promotion) {
// TODO: Implement clear() method.
}
}
......@@ -17,7 +17,7 @@ use Drupal\Tests\UnitTestCase;
class GetAffectedEntitiesTest extends UnitTestCase {
/**
* Test that we do not get a query when passing an offer not supporting it.
* Test that we do not get a query when passing a promotion not supporting it.
*/
public function testUnsupportedPromotion() {
$manager = new AffectedEntitiesManager();
......@@ -28,7 +28,7 @@ class GetAffectedEntitiesTest extends UnitTestCase {
}
/**
* Test that we do not get a query when passing an offer not supporting it.
* Test that we get a query when passing a promotion supporting it.
*/
public function testsupportedPromotion() {
$manager = new AffectedEntitiesManager();
......@@ -48,4 +48,32 @@ class GetAffectedEntitiesTest extends UnitTestCase {
$this->assertNotEquals(FALSE, $manager->getAffectedEntitiesQuery($mock_promotion, 'commerce_product'));
}
/**
* Test that we do not get a query when passing an offer not supporting it.
*/
public function testUnsupportedOffer() {
$manager = new AffectedEntitiesManager();
$offer = new DummyPromotionNoInterface();
$this->assertEquals(FALSE, $manager->getAffectedEntitiesQueryByOffer($offer, 'commerce_product'));
}
/**
* Test that we get a query when passing an offer supporting it.
*/
public function testsupportedOffer() {
$manager = new AffectedEntitiesManager();
$offer = new DummyPromotionWithInterface();
// Inject the database service into the container.
$container = new ContainerBuilder();
$select = $this->createMock(SelectInterface::class);
$select->method('fields')
->willReturn($select);
$db = $this->createMock(Connection::class);
$db->method('select')
->willReturn($select);
$container->set('database', $db);
\Drupal::setContainer($container);
$this->assertNotEquals(FALSE, $manager->getAffectedEntitiesQueryByOffer($offer, 'commerce_product'));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment