diff --git a/tests/cartitem_test.php b/tests/cartitem_test.php index 8b3e401..ee8f31c 100644 --- a/tests/cartitem_test.php +++ b/tests/cartitem_test.php @@ -104,4 +104,46 @@ public function test_as_array_contains_all_fields(): void { $this->assertArrayHasKey($property->getName(), $cartitemarray); } } + + /** + * Test costcenter not set + * @covers \cartitem->costcenter + * + * @return void + */ + public function test_costcenter_is_set(): void { + $price = 10.00; + $cartitem = new cartitem( + 1, + 'Testitem 1', + $price, + get_config('local_shopping_cart', 'globalcurrency') ?? 'EUR', + 'local_shopping_cart', + 'main', + 'My Testitem 1 description' + ); + + $this->assertNull($cartitem->costcenter); + + $costcenter = 'TestitemCostcenter'; + $cartitem = new cartitem( + 1, + 'Testitem 1', + $price, + get_config('local_shopping_cart', 'globalcurrency') ?? 'EUR', + 'local_shopping_cart', + 'main', + 'My Testitem 1 description', + '', + null, + null, + null, + null, + 0, + $costcenter + ); + + $this->assertEquals($costcenter, $cartitem->costcenter); + } + } diff --git a/tests/cartstore_test.php b/tests/cartstore_test.php index f602939..16c0be6 100644 --- a/tests/cartstore_test.php +++ b/tests/cartstore_test.php @@ -140,6 +140,56 @@ public function test_cartstore_add_items(): void { $this->assertEquals($data["initialtotal"], 47.63); } + /** + * Test test_cartstore_get_costcenter + * @covers \cartstore + */ + public function test_cartstore_get_costcenter(): void { + + $user1 = $this->get_data_generator()->create_user(); + + $cartstore = cartstore::instance((int)$user1->id); + + shopping_cart::add_item_to_cart( + 'local_shopping_cart', + 'testitem', + 1, + $user1->id); + + shopping_cart::add_item_to_cart( + 'local_shopping_cart', + 'testitem', + 2, + $user1->id); + + $data = $cartstore->get_data(); + + // Check total price and absence of costcenter and credits. + $this->assertEquals(33.83, $data['price']); + $this->assertEquals(30.3, $data['price_net']); + $this->assertEquals(33.83, $data['initialtotal']); + $this->assertEmpty($data['costcenter']); + $this->assertEmpty($data['deductible']); + $this->assertEmpty($data['remainingcredit']); + $this->assertArrayHasKey('items', $data); + $this->assertEquals(2, count($data['items'])); + + // Set default costcenter and credits for it. + set_config('defaultcostcenterforcredits', 'CostCenter1', 'local_shopping_cart'); + shopping_cart_credits::add_credit($user1->id, 50, 'EUR', 'CostCenter1'); + $data = $cartstore->get_data(); + + // Check total price, costcenter and credits. + $this->assertEmpty($data['price']); + $this->assertEquals(30.3, $data['price_net']); + $this->assertEquals(33.83, $data['deductible']); + $this->assertEquals(33.83, $data['initialtotal']); + $this->assertEquals(16.17, $data['remainingcredit']); + $this->assertEquals('CostCenter1', $data['costcenter']); + $this->assertArrayHasKey('items', $data); + $this->assertEquals(2, count($data['items'])); + } + /** * Data provider for test_cartstore_get_data *