Skip to content

Commit

Permalink
テストコードを修正
Browse files Browse the repository at this point in the history
  • Loading branch information
takeuji committed Jun 28, 2023
1 parent 1d1dad0 commit 819d0df
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 40 deletions.
63 changes: 42 additions & 21 deletions tests/Eccube/Tests/Entity/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public function testGetTotalByTaxRate()
{
$Order = $this->createTestOrder();

self::assertSame(790187, $this->getTaxableTotal(), '課税合計');
self::assertSame(65160.0, $Order->getTotalByTaxRate()[8], '8%対象値引き後合計');
self::assertSame(717868.0, $Order->getTotalByTaxRate()[10], '10%対象値引き後合計');
}
Expand All @@ -250,6 +251,27 @@ public function testGetTaxByTaxRate()
}

protected function createTestOrder()
{
$data = $this->getOrderItemData();
$Order = new Order();
foreach ($data as $row) {
$OrderItem = new OrderItem();
$OrderItem->setTaxType($row[0]);
$OrderItem->setTaxRate($row[1]);
$OrderItem->setPrice($row[2]);
$OrderItem->setTax($row[3]);
$OrderItem->setQuantity($row[4]);
$OrderItem->setOrderItemType($row[5]);
$OrderItem->setTaxDisplayType($row[6]);
$OrderItem->setRoundingType($row[7]);

$Order->addOrderItem($OrderItem);
}

return $Order;
}

protected function getOrderItemData()
{
$Taxation = $this->entityManager->find(TaxType::class, TaxType::TAXATION);
$NonTaxable = $this->entityManager->find(TaxType::class, TaxType::NON_TAXABLE);
Expand All @@ -267,32 +289,31 @@ protected function createTestOrder()

// 税率ごとに金額を集計する
$data = [
[$Taxation, 10, 71141, round(71141 * (10/100)), 5, $ProductItem, $TaxExcluded, $RoundingType], // 商品明細
[$Taxation, 10, 92778, round(92778 * (10/100)), 4, $ProductItem, $TaxExcluded, $RoundingType], // 商品明細
[$Taxation, 8, 15221, round(15221 * (8/100)), 5, $ProductItem, $TaxExcluded, $RoundingType], // 商品明細
[$Taxation, 10, -71141, round(-71141 * (10/100)), 1, $DiscountItem, $TaxExcluded, $RoundingType], // 課税値引き
[$Taxation, 8, -15221, round(-15221 * (8/100)), 1, $DiscountItem, $TaxExcluded, $RoundingType], // 課税値引き
[$Taxation, 10, 1000, round(1000 * (10/100)), 1, $DeliveryFee, $TaxIncluded, $RoundingType], // 送料
[$Taxation, 10, 2187, round(1000 * (10/100)), 1, $Charge, $TaxIncluded, $RoundingType], // 手数料
[$Taxation, 10, 71141, round(71141 * (10 / 100)), 5, $ProductItem, $TaxExcluded, $RoundingType], // 商品明細
[$Taxation, 10, 92778, round(92778 * (10 / 100)), 4, $ProductItem, $TaxExcluded, $RoundingType], // 商品明細
[$Taxation, 8, 15221, round(15221 * (8 / 100)), 5, $ProductItem, $TaxExcluded, $RoundingType], // 商品明細
[$Taxation, 10, -71141, round(-71141 * (10 / 100)), 1, $DiscountItem, $TaxExcluded, $RoundingType], // 課税値引き
[$Taxation, 8, -15221, round(-15221 * (8 / 100)), 1, $DiscountItem, $TaxExcluded, $RoundingType], // 課税値引き
[$Taxation, 10, 1000, round(1000 * (10 / 100)), 1, $DeliveryFee, $TaxIncluded, $RoundingType], // 送料
[$Taxation, 10, 2187, round(1000 * (10 / 100)), 1, $Charge, $TaxIncluded, $RoundingType], // 手数料
[$NonTaxable, 0, -7000, 0, 1, $DiscountItem, $TaxIncluded, $RoundingType], // 不課税明細
[$TaxExempt, 0, -159, 0, 1, $DiscountItem, $TaxIncluded, $RoundingType], // 非課税明細
];

$Order = new Order();
foreach ($data as $row) {
$OrderItem = new OrderItem();
$OrderItem->setTaxType($row[0]);
$OrderItem->setTaxRate($row[1]);
$OrderItem->setPrice($row[2]);
$OrderItem->setTax($row[3]);
$OrderItem->setQuantity($row[4]);
$OrderItem->setOrderItemType($row[5]);
$OrderItem->setTaxDisplayType($row[6]);
$OrderItem->setRoundingType($row[7]);
return $data;
}

$Order->addOrderItem($OrderItem);
}
protected function getTaxableTotal()
{
$data = $this->getOrderItemData();
$Taxation = $this->entityManager->find(TaxType::class, TaxType::TAXATION);
$TaxExcluded = $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::EXCLUDED);
$taxableItem = array_filter($data, function ($item) use ($Taxation) {
return $item[0] === $Taxation;
});

return $Order;
return array_reduce($taxableItem, function ($sum, $item) use ($TaxExcluded) {
return $sum + ($item[2] + $item[6] !== $TaxExcluded ? $item[3] : 0) * $item[4];
});
}
}
61 changes: 42 additions & 19 deletions tests/Eccube/Tests/Web/Admin/Order/EditControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
use Eccube\Entity\Customer;
use Eccube\Entity\MailHistory;
use Eccube\Entity\Master\Job;
use Eccube\Entity\Master\Sex;
use Eccube\Entity\Master\OrderStatus;
use Eccube\Entity\Master\RoundingType;
use Eccube\Entity\Master\Sex;
use Eccube\Entity\Master\TaxType;
use Eccube\Entity\Order;
use Eccube\Entity\Product;
use Eccube\Entity\ProductClass;
Expand Down Expand Up @@ -174,23 +175,24 @@ public function testNotUpdateLastBuyDate()
* ・ <script> スクリプトインジェクション
*
* @see https://github.com/EC-CUBE/ec-cube/issues/5372
*
* @return void
*/
public function testOrderMailXSSAttackPrevention()
{
// Create a new news item for the homepage with a XSS attack (via <script> AND id attribute injection)
$Customer = $this->createCustomer();
$Order = $this->createOrder($Customer);
$MailHistory = (New MailHistory())->setMailHtmlBody(
"<div id='dangerous-id' class='safe_to_use_class'>
$MailHistory = (new MailHistory())->setMailHtmlBody(
"<div id='dangerous-id' class='safe_to_use_class'>
<p>メール内容#1</p>
<script>alert('XSS Attack')</script>
<a href='https://www.google.com'>safe html</a>
</div>"
)
->setOrder($Order)
->setMailSubject("テスト")
->setMailBody("テスト内容")
->setMailSubject('テスト')
->setMailBody('テスト内容')
->setSendDate(new \DateTime())
->setCreator($this->createMember());
$this->entityManager->persist($MailHistory);
Expand Down Expand Up @@ -448,8 +450,8 @@ public function testOrderProcessingWithTax()
// 管理画面から受注登録
$this->client->request(
'POST', $this->generateUrl('admin_order_edit', ['id' => $Order->getId()]), [
'order' => $formData,
'mode' => 'register',
'order' => $formData,
'mode' => 'register',
]
);

Expand All @@ -458,27 +460,48 @@ public function testOrderProcessingWithTax()
$EditedOrder = $this->orderRepository->find($Order->getId());
$formDataForEdit = $this->createFormDataForEdit($EditedOrder);

//税金計算
$totalTax = 0;
foreach ($formDataForEdit['OrderItems'] as $indx => $orderItem) {
//商品数変更3個追加
$formDataForEdit['OrderItems'][$indx]['quantity'] = $orderItem['quantity'] + 3;
$tax = static::getContainer()->get(TaxRuleService::class)->getTax($orderItem['price']);
$totalTax += $tax * $formDataForEdit['OrderItems'][$indx]['quantity'];
$addingQuantity = 3;
foreach ($formDataForEdit['OrderItems'] as $index => $orderItem) {
// 商品数変更3個追加
$formDataForEdit['OrderItems'][$index]['quantity'] = $orderItem['quantity'] + $addingQuantity;
}

// 管理画面で受注編集する
$this->client->request(
'POST', $this->generateUrl('admin_order_edit', ['id' => $Order->getId()]), [
'order' => $formDataForEdit,
'mode' => 'register',
'order' => $formDataForEdit,
'mode' => 'register',
]
);

$this->assertTrue($this->client->getResponse()->isRedirect($this->generateUrl('admin_order_edit', ['id' => $Order->getId()])));
$EditedOrderafterEdit = $this->orderRepository->find($Order->getId());

//確認する「トータル税金」
// 税金計算
$taxableItem = array_filter($EditedOrder->getOrderItems()->toArray(), function ($OrderItem) {
return !is_null($OrderItem->getTaxType()) && $OrderItem->getTaxType()->getId() === TaxType::TAXATION;
});
$totalTaxByTaxRate = [];
$totalByTaxRate = [];
foreach ($taxableItem as $OrderItem) {
$totalPrice = $OrderItem->getPriceIncTax() * ($OrderItem->getQuantity() + $addingQuantity);
$taxRate = $OrderItem->getTaxRate();
$totalByTaxRate[$taxRate] = isset($totalByTaxRate[$taxRate])
? $totalByTaxRate[$taxRate] + $totalPrice
: $totalPrice;
}
foreach ($totalByTaxRate as $rate => $price) {
$tax = static::getContainer()->get(TaxRuleService::class)
->roundByRoundingType($price * ($rate / (100 + $rate)), \Eccube\Entity\Master\RoundingType::ROUND);
$totalTaxByTaxRate[$rate] = $tax;
}
$totalTax = array_reduce($totalTaxByTaxRate, function ($sum, $tax) {
$sum += $tax;

return $sum;
}, 0);

// 確認する「トータル税金」
$this->expected = $totalTax;
$this->actual = $EditedOrderafterEdit->getTax();
$this->verify();
Expand Down Expand Up @@ -597,8 +620,8 @@ public function testUpdateShippingDeliveryTimeToNoneSpecified()
// 管理画面で受注編集する
$this->client->request(
'POST', $this->generateUrl('admin_order_edit', ['id' => $Order->getId()]), [
'order' => $formDataForEdit,
'mode' => 'register',
'order' => $formDataForEdit,
'mode' => 'register',
]
);
$this->assertTrue($this->client->getResponse()->isRedirect($this->generateUrl('admin_order_edit', ['id' => $Order->getId()])));
Expand Down

0 comments on commit 819d0df

Please sign in to comment.