Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHPUnit test only] Adding in assertions re: Line Item and Contribution data-integrity. #12611

Merged
merged 4 commits into from
Aug 5, 2018

Conversation

KarinG
Copy link
Contributor

@KarinG KarinG commented Aug 1, 2018

Overview

In 4.7.27 - one of our large professional association clients observed a data integrity issue were after Edit Contribution the Line Item subtotals + their Tax Amount no longer added up to Contribution total_amount [not talking rounding errors - June 2018 is off by > $9.5k]. The good news is that we've not yet observed it in 5.3.x since. However I could not find any tests that would secure/lock in data integrity between Line Items and Contribution totals.

Before

Unit test testLineItemAmountOnSaleTax -> checks that $lineItem['qty'] and $lineItem['tax_amount'] are unaffected upon a simple Save of the Edit Contribution form;

After

Unit test testLineItemAmountOnSaleTax -> now also checks that $lineItem['unit_price'] and $lineItem['line_total'] are also unaffected upon a simple Save of the Edit Contribution form;

In addition
Unit test testLineItemAmountOnSaleTax -> now also checks that $contribution['total_amount'] = $lineItem['line_total'] + $lineItem['tax_amount'];

In addition
Unit test testLineItemAmountOnSaleTax -> now also checks that $contribution['tax_amount'] = $lineItem['tax_amount'];

Technical Details

The tests I've added Fail on my local/buildkit - the unit_price and line_total are 55.00 after saving the Contribution Form [before saving the form -> lines 1323 - 1324 they were 50.00] - see screenshot:

image

Comments

Of course I realize we can't merge in a failing test - so want to here:
a) check to see if Jenkins agrees
b) get consensus that the additional assertions here are correct

@civibot
Copy link

civibot bot commented Aug 1, 2018

(Standard links)

@@ -1339,8 +1339,18 @@ public function testLineItemAmountOnSaleTax() {
// ensure that the line-item values got unaffected
$lineItem = $this->callAPISuccessGetSingle('LineItem', array('entity_id' => $membership['id'], 'entity_table' => 'civicrm_membership'));
$this->assertEquals(1, $lineItem['qty']);
$this->assertEquals(50.00, $lineItem['unit_price']);
$this->assertEquals(50.00, $lineItem['line_total']);
$this->assertEquals(5.00, $lineItem['tax_amount']); // ensure that tax amount is not changed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also check if $lineItem['unit_price'] and $lineItem['line_total'] have not changed on Resubmitting the Edit Contribution form.

);
$this->assertEquals($contribution['total_amount'], $lineItem['line_total'] + $lineItem['tax_amount']);
$this->assertEquals($contribution['tax_amount'], $lineItem['tax_amount']);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the data-integrity part - we must ensure things add up. Since there is only 1 Line Item in this test we don't have to worry about summation over Line Items.

@KarinG
Copy link
Contributor Author

KarinG commented Aug 1, 2018

Jenkins - failed -> expected:
(first assertion that fails is):

CRM_Member_Form_MembershipTest::testLineItemAmountOnSaleTax
Failed asserting that '55.00' matches expected 50.0.

@eileenmcnaughton
Copy link
Contributor

@KarinG so we obviously can't merge this because the test it adds doesn't pass....

What happens if you comment out that line? Sometimes the path to getting tests merged is to comment out the parts that still need fixing & then they can be re-enabled when the fix follows

@jusfreeman ping

@KarinG
Copy link
Contributor Author

KarinG commented Aug 1, 2018

Thanks @eileenmcnaughton - Absolutely.

3 of the 4 assertions that I injected fail (and they should not fail - so three would need to be commented out):
$this->assertEquals(50.00, $lineItem['unit_price']);
$this->assertEquals(50.00, $lineItem['line_total']);
$this->assertEquals($contribution['total_amount'], $lineItem['line_total'] + $lineItem['tax_amount']);

1 of 4 assertions that I injected passes (correctly):
$this->assertEquals($contribution['tax_amount'], $lineItem['tax_amount']);

I'm hoping we can put our heads together towards making Sales Tax Math testable. Some of the Math obviously happens in the GUI layers (and it shouldn't).

ping @monishdeb and @mattwire as well.

@eileenmcnaughton
Copy link
Contributor

OK cool - so I guess for this PR the main outcome is to trigger discussion & focus on those issues. Let's keep the PR open to facilitate discussion for a bit & then close & drop back to tracking via gitlab it it doesn't become mergeable in the next couple of weeks

@KarinG
Copy link
Contributor Author

KarinG commented Aug 1, 2018

If we get consensus on these Assertions - perhaps we can merge them in commented out - with a note;

@eileenmcnaughton
Copy link
Contributor

That works - let's see if people agree on the assertations

@KarinG
Copy link
Contributor Author

KarinG commented Aug 2, 2018

I did some research and found that @monishdeb intended to inject at least 2 of the 3 assertions that I'm now trying to provisionally inject - earlier this year:

https://gist.github.com/monishdeb/ab5d62fb6799f4e3d99f8e75432a64ca

image

However the commit that went into the code base is omitting two previously gist-ed assertions: re: lineItem['unit_price'] and lineItem['line_total'] https://github.com/civicrm/civicrm-core/pull/11655/files

image

I understand why these assertions had to be omitted at that time - because those assertions fail (off by $5).

Context was a PR by @mattwire : #11461

My thoughts:

  • I think we want to be able to make these assertions in phpunit tests? If yes can we provisionally commit this PR with // commented out and make that our goal - that we can lock in a test that ensures lineItem $ add up to contribution $
  • The GUI (using CiviCRM itself) is fine - which means somewhere else there is a $5 offset to make two wrongs right. I can dig in and look into that if anyone finds that is the next step here?
  • I'm not trying to solve a bug for a specific client - trying to help prevent more in future.

@mlutfy
Copy link
Member

mlutfy commented Aug 3, 2018

+1 more tests in this area of the code.

+1 merge them commented-in, while work is done to figure out why the form behaves differently when used from the GUI?

Do we have a Gitlab issue for the failing tests? It would be important to cross-reference it in the code. Otherwise they'll probably get cleaned out at some point.

@JoeMurray
Copy link
Contributor

I think that at a high level the contribution total should equal the sum of (the qty*amount for each line item plus tax on line item). I focus on the financial_item and financial_trxn tables and am less confident knowing how accurately the tax in line item tracks the accounting value stored in financial_item.

+1 for more tests in this area.

@eileenmcnaughton
Copy link
Contributor

@mlutfy I think rather than a gitlab issue the commented out lines need a good chunk of code comments defending their existence & explaining whey then need to be fixed rather than than removed by cleaner-uppers

@KarinG sounds like you should update this to be commented out + commented & we can merge this & we'll at least get one extra assertion for now

@KarinG
Copy link
Contributor Author

KarinG commented Aug 4, 2018

@eileenmcnaughton - I've made the edits - do you think the wording is clear?

@eileenmcnaughton
Copy link
Contributor

@KarinG yes - I think this helps - merging

Interestingly I took a small foray into the membership code & found straight away an inconsistency on tax handling in membership renewal vs membership https://github.com/civicrm/civicrm-core/pull/12593/files#diff-349ca8bcf1ef264bf3ca9acc2593aa54R707

@KarinG
Copy link
Contributor Author

KarinG commented Aug 5, 2018

@eileenmcnaughton - thank you for merging this and for any steps aiming to consolidate Tax Code.

$taxRate = $this->getTaxRateForFinancialType($values['financial_type_id']);

That should be a reliable way to get the taxRate - and should probably become the standard - I think @monishdeb wrote that and is already using that in many places.

@eileenmcnaughton eileenmcnaughton merged commit 1f6feee into civicrm:master Aug 5, 2018
eileenmcnaughton added a commit that referenced this pull request Aug 7, 2018
Follow up on #12611 - adding in three data-integrity assertions.
mattwire pushed a commit to mattwire/civicrm-core that referenced this pull request Aug 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants