<?php

require __DIR__ . '/../vendor/autoload.php';
use FUnit as fu;
use Knlv\Zf2\Validator\NotIdentical;
fu::setup(function () {
    $validator = new NotIdentical();
    fu::fixture('validator', $validator);
});
fu::test('Test not_identical validates correctly', function () {
    $validator = fu::fixture('validator');
    $validator->setToken('token');
    fu::not_ok($validator->isValid('value', array('token' => 'value')), 'Assert validator returns false on same');
    fu::ok($validator->isValid('value', array('token' => 'other')), 'Assert validator retuns true on different');
    fu::ok($validator->isValid('value'), 'Assert validator returns true if no context provided');
    fu::ok($validator->isValid('value', array('other_token' => 'value')), 'Assert validator returns true if token not found in context');
    $validator->setToken(null);
    fu::not_ok($validator->isValid('value', array('token' => 'value')), 'Assert validator return false if no token is set');
});
fu::test('Test not_identical messages', function () {
    $validator = fu::fixture('validator');
    $validator->setToken('token');
    $validator->isValid('value', array('token' => 'value'));
    fu::has($validator::SAME, $validator->getMessages(), 'Assert same message');
    $validator->isValid('value', array('token' => 'other'));
    $messages = $validator->getMessages();
    fu::ok(empty($messages), 'Assert empty messages if validator validates');
    $validator->setToken(null);
    $validator->isValid('value', array('token' => 'value'));
    fu::has($validator::MISSING_TOKEN, $validator->getMessages(), 'Assert missing token message');
});
<?php

require __DIR__ . '/../vendor/autoload.php';
use FUnit as fu;
use Knlv\Zf2\Validator\ExplodeWithContext;
use Zend\Validator\Callback;
// Zend\Validator\Callback uses context in isValid method in order to test
fu::setup(function () {
    $validator = new ExplodeWithContext();
    fu::fixture('validator', $validator);
});
fu::test('Test explode_with_context passes context', function () {
    $validator = fu::fixture('validator');
    $validator->setValueDelimiter(',');
    $validator->setValidator(new Callback(function ($value, $context) {
        fu::equal('value', $context, 'Assert context pass');
        return false;
    }));
    $validator->isValid('value 1,value 2,other value', 'value');
});
fu::test('Test explode_with_context validates correctly', function () {
    $validator = fu::fixture('validator');
    $validator->setValueDelimiter('|');
    $validator->setValidator(new Callback(function ($value, $context) {
        // value begins with value from context
        return 0 === strpos($value, $context);
    }));
    fu::ok($validator->isValid('value 1|value 2|value 3', 'value'), 'Assert returns true if validates');
    fu::not_ok($validator->isValid('value 1|other value 2|value 3', 'value'), 'Assert returns false if one does not validate');
});
use Zend\Permissions\Acl\Acl;
use Knlv\Zf2\Permissions\Acl\Assertion\Callback as AclCallback;
fu::setup(function () {
    $acl = new Acl();
    // add roles
    $acl->addRole('guest');
    $acl->addRole('member', 'guest');
    // add resources
    $acl->addResource('article');
    // add rules
    $acl->allow('guest', 'article', array('read'));
    $acl->allow('member', 'article', array('write', 'delete'));
    fu::fixture('acl', $acl);
});
fu::test('Test acl callback assertion', function () {
    $acl = fu::fixture('acl');
    $test = $acl->isAllowed('guest', 'article', 'read') && $acl->isAllowed('member', 'article', 'read') && $acl->isAllowed('member', 'article', 'write') && !$acl->isAllowed('guest', 'article', 'write');
    fu::ok($test, 'Test acl without assertions');
    $assertTrue = new AclCallback(function () {
        return true;
    });
    $assertFalse = new AclCallback(function () {
        return false;
    });
    $acl->removeAllow('member', 'article', 'write');
    $acl->allow('member', 'article', 'write', $assertFalse);
    fu::not_ok($acl->isAllowed('member', 'article', 'write'), 'Assert not allowed when callback returns false');
    $acl->removeAllow('member', 'article', 'write');
    $acl->allow('member', 'article', 'write', $assertTrue);
    fu::ok($acl->isAllowed('member', 'article', 'write'), 'Assert allowed when callback returns true');
});
    $data = array(array('foo' => ' bazbat ', 'bar' => ' 12345 ', 'baz' => ''), array('foo' => ' bazbar ', 'bar' => ' 12345 ', 'baz' => ''));
    $expected = array(array('foo' => 'bazbat', 'bar' => '12345', 'baz' => ''), array('foo' => 'bazbar', 'bar' => '12345', 'baz' => ''));
    $collectionFilter = fu::fixture('collectionFilter');
    $collectionFilter->setData($data);
    $collectionFilter->setUniqueFields(array('foo'));
    fu::ok($collectionFilter->isValid(), 'Assert inputfilter is valid');
    $messages = $collectionFilter->getMessages();
    fu::ok(empty($messages), 'Assert inputfilter has no error messages');
    fu::equal($expected, $collectionFilter->getValues(), 'Assert inputfilter returns expected values');
    $collectionFilter->setUniqueFields(array('bar'));
    fu::not_ok($collectionFilter->isValid(), 'Assert inputfilter is invalid');
    $messages = $collectionFilter->getMessages();
    fu::ok(isset($messages[1]['bar'][$collectionFilter::NOT_UNIQUE]), 'Assert message for not unique isset');
    fu::equal($expected, $collectionFilter->getValues(), 'Assert inputfilter returns expected values');
});
fu::test('Test inputfilter validate for unique values in fields on invalid data', function () {
    $data = array(array('foo' => ' bazbatfoo ', 'bar' => ' 12345 ', 'baz' => ''), array('foo' => ' bazbatfoo ', 'bar' => ' 54321 ', 'baz' => ''));
    $expected = array(array('foo' => 'bazbatfoo', 'bar' => '12345', 'baz' => ''), array('foo' => 'bazbatfoo', 'bar' => '54321', 'baz' => ''));
    $collectionFilter = fu::fixture('collectionFilter');
    $collectionFilter->setData($data);
    $collectionFilter->setUniqueFields(array('bar'));
    fu::not_ok($collectionFilter->isValid(), 'Assert inputfilter is invalid');
    $messages = $collectionFilter->getMessages();
    fu::ok(isset($messages[0]['foo'][Validator\StringLength::TOO_LONG]), 'Assert correct error messages');
    fu::equal($expected, $collectionFilter->getValues(), 'Assert inputfilter returns expected values');
    $collectionFilter->setUniqueFields(array('foo'));
    fu::not_ok($collectionFilter->isValid(), 'Assert inputfilter is invalid');
    $messages = $collectionFilter->getMessages();
    fu::ok(isset($messages[0]['foo'][Validator\StringLength::TOO_LONG]) && isset($messages[1]['foo'][Validator\StringLength::TOO_LONG]) && isset($messages[1]['foo'][$collectionFilter::NOT_UNIQUE]), 'Assert message for not unique isset along with other');
    fu::equal($expected, $collectionFilter->getValues(), 'Assert inputfilter returns expected values');
});
<?php

require __DIR__ . '/../vendor/autoload.php';
use FUnit as fu;
use Zend\Permissions\Rbac\Rbac;
use Knlv\Zf2\Permissions\Rbac\Assertion\Callback as RbacCallback;
fu::setup(function () {
    $rbac = new Rbac();
    $rbac->addRole('member');
    $rbac->addRole('guest', 'member');
    $rbac->getRole('guest')->addPermission('read');
    $rbac->getRole('member')->addPermission('write');
    fu::fixture('rbac', $rbac);
});
fu::test('Test rbac callback assertion', function () {
    $rbac = fu::fixture('rbac');
    $test = $rbac->isGranted('guest', 'read') && $rbac->isGranted('member', 'read') && !$rbac->isGranted('guest', 'write') && $rbac->isGranted('member', 'write');
    fu::ok($test, 'Test rbac without assertions');
    $assertTrue = new RbacCallback(function () {
        return true;
    });
    $assertFalse = new RbacCallback(function () {
        return false;
    });
    fu::not_ok($rbac->isGranted('member', 'read', $assertFalse), 'Assert permission not granted when callback returns false');
    fu::ok($rbac->isGranted('member', 'write', $assertTrue), 'Assert permission granted when callback returns true');
});