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

Correct Portugal validation algorithm #57

Merged
merged 2 commits into from
Jun 11, 2020
Merged

Correct Portugal validation algorithm #57

merged 2 commits into from
Jun 11, 2020

Conversation

SLourenco
Copy link

The Portugal validation algorithm has a bug in an if code block that is never run. In

public function validate(string $id): bool
    {
        $id = $this->sanitize($id);

        $sum = 0;
        $toggleDigit = false;

        for ($i = 11; $i >= 0; $i--) {
            $value = $this->getCharacterValue($id[$i]);

            if ($toggleDigit) {
                $value *= 2;

                if ($value > 9) {
                    $value -= 9;
                }

                $sum += $value;
                $toggleDigit = !$toggleDigit;
            }
        }

        return ($sum % 10) === 0;
    }

the $toggleDigit is initialized to false and is not modified outside of the if statement, which depends on it being true to run.

@AlexOlival AlexOlival self-assigned this Jun 11, 2020
@AlexOlival AlexOlival self-requested a review June 11, 2020 11:22
@AlexOlival AlexOlival added the bug Something isn't working label Jun 11, 2020
@AlexOlival
Copy link
Collaborator

Thank you very very much!

You ended up exposing a problem in our Validation algorithm tests.
Our assertions were:

public function test_validation_behaviour(): void
{
    foreach ($this->validIds as $id) {
        $this->assertTrue(
            Socrates::validateId($id, 'PT')
        );
    }
    
    // Here be dragons...
    $this->expectException(InvalidLengthException::class);

    Socrates::validateId('11084129 8 ZX', 'PT');

    // Everything here passes...
    $this->assertFalse(
        Socrates::validateId('14897475 4 ZY5', 'PT')
    );
}

Not only some countries test for a single invalid ID - which isn't ideal - but even those that test with multiple of those have that same problem.

I went ahead and changed the order of assertions and also made it test against an array of Invalid IDs:

public function test_validation_behaviour(): void
{
    foreach ($this->validIds as $id) {
        $this->assertTrue(
            Socrates::validateId($id, 'PT')
        );
    }

    foreach ($this->invalidIds as $invalidId) {
        $this->assertFalse(
            Socrates::validateId($invalidId, 'PT')
        );
    }

    $this->expectException(InvalidLengthException::class);

    Socrates::validateId('11084129 8 ZX', 'PT');
}

Also, I opened an issue #59 for us to go ahead and fix our test suite.
Sounds like a new release is in order @JoaoFSCruz

Copy link
Collaborator

@JoaoFSCruz JoaoFSCruz left a comment

Choose a reason for hiding this comment

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

Nice catch!

@AlexOlival AlexOlival merged commit 6c5bd66 into reducktion:master Jun 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants