Skip to content

Commit

Permalink
add 3 phpunit test on costcenters in testitems and cartstore (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
semteacher committed Nov 2, 2024
1 parent 8a82d40 commit c2c0b0a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/cartitem_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
50 changes: 50 additions & 0 deletions tests/cartstore_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit c2c0b0a

Please sign in to comment.