function testMatchRuleFailure()
  {
    $this->validator->addRule(new MatchRule('testfield', 'testmatch'));

    $data = new Dataspace();
    $data->set('testfield', 'peaches');
    $data->set('testmatch', 'cream');

    $this->error_list->expectOnce('addError', array('testfield', Strings :: get('error_no_match', 'error'), array('match_field' => 'testmatch')));

    $this->validator->validate($data);
    $this->assertFalse($this->validator->isValid());
  }
  function testRequiredRuleFalse()
  {
    $this->validator->addRule(new RequiredRule('testfield'));

    $data = new Dataspace();
    $data->set('testfield', false);

    $this->error_list->expectNever('addError');

    $this->validator->validate($data);
    $this->assertTrue($this->validator->isValid());
  }
  function testTreeIdentifierRuleError()
  {
    $this->validator->addRule(new TreePathRule('test'));

    $data = new Dataspace();
    $data->set('test', '/root/document/1');

    $this->ErrorList->expectOnce('addError', array('validation', 'ERROR_INVALID_TREE_PATH', array('Field' => 'test'), NULL));

    $this->validator->validate($data);
    $this->assertFalse($this->validator->isValid());
  }
  function testCanadaZipRuleInvalid3()
  {
    $this->validator->addRule(new CanadaZipRule('test'));

    $data = new Dataspace();
    $data->set('test', 'H2V2K1');

    $this->error_list->expectOnce('addError', array('test', Strings :: get('error_invalid_zip_format', 'error'), array()));

    $this->validator->validate($data);
    $this->assertFalse($this->validator->isValid());
  }
  function testUrlRuleInvalid()
  {
    $this->validator->addRule(new UrlRule('testfield'));

    $data = new Dataspace();
    $data->set('testfield', '://not/a/valid/url');

    $this->error_list->expectOnce('addError', array('testfield', 'BAD_URL', array()));

    $this->validator->validate($data);
    $this->assertFalse($this->validator->isValid());
  }
  function testLocaleDateRuleError()
  {
    $this->validator->addRule(new LocaleDateRule('test', 'en'));

    $data = new Dataspace();
    $data->set('test', '02jjklklak/sdsdskj34-sdsdsjkjkj78');

    $this->ErrorList->expectOnce('addError', array('validation', 'INVALID_DATE', array('Field' => 'test'), NULL));

    $this->validator->validate($data);
    $this->assertFalse($this->validator->isValid());
  }
  function testCanadaZipRuleInvalid3()
  {
    $this->validator->addRule(new CanadaZipRule('test'));

    $data = new Dataspace();
    $data->set('test', 'H2V2K1');

    $this->ErrorList->expectOnce('addError', array('validation', 'ERROR_INVALID_ZIP_FORMAT', array('Field' => 'test'), NULL));

    $this->validator->validate($data);
    $this->assertFalse($this->validator->isValid());
  }
  function testNotValidContainsSlash()
  {
    $this->validator->addRule(new IdentifierRule('test'));

    $data = new Dataspace();
    $data->set('test', 'test/test');

    $this->ErrorList->expectOnce('addError', array('validation', 'INVALID', array('Field' => 'test'), NULL));

    $this->validator->validate($data);
    $this->assertFalse($this->validator->isValid());
  }
  function testTreeIdentifierRuleError()
  {
    $this->validator->addRule(new TreePathRule('test'));

    $data = new Dataspace();
    $data->set('test', '/root/document/1');

    $this->error_list->expectOnce('addError', array('test', Strings :: get('error_invalid_tree_path', 'error'), array()));

    $this->validator->validate($data);
    $this->assertFalse($this->validator->isValid());
  }
  function testUniqueUserEmailRuleCorrectEdit()
  {
    $this->validator->addRule(new UniqueUserEmailRule('test', '2@2.2'));

    $data = new Dataspace();
    $data->set('test', '2@2.2');

    $this->ErrorList->expectNever('addError');

    $this->validator->validate($data);
    $this->assertTrue($this->validator->isValid());
  }
  function testSizeRangeRuleBlank()
  {
    $this->validator->addRule(new SizeRangeRule('testfield', 5, 10));

    $data = new Dataspace();
    $data->set('testfield', '');

    $this->error_list->expectNever('addError');

    $this->validator->validate($data);
    $this->assertTrue($this->validator->isValid());
  }
 function testFetchAllByPath()
 {
     $toolkit =& Limb::toolkit();
     $request =& $toolkit->getRequest();
     $uri =& $request->getUri();
     $uri->setPath($path1 = '/about/news/1');
     $object1 = new Dataspace();
     $object1->set('title', $title1 = 'title1');
     $object2 = new Dataspace();
     $object2->set('title', $title2 = 'title2');
     $object3 = new Dataspace();
     $object3->set('title', $title3 = 'title1');
     $this->db->insert('sys_object', array('oid' => $oid1 = 100, 'class_id' => $class_id1 = 1));
     $this->db->insert('sys_object', array('oid' => $oid2 = 101, 'class_id' => $class_id2 = 2));
     $this->db->insert('sys_object', array('oid' => $oid3 = 102, 'class_id' => $class_id3 = 3));
     $this->db->insert('sys_class', array('id' => $class_id1, 'name' => $class_name1 = 'class_name1'));
     $this->db->insert('sys_class', array('id' => $class_id2, 'name' => $class_name2 = 'class_name2'));
     $this->db->insert('sys_class', array('id' => $class_id3, 'name' => $class_name3 = 'class_name3'));
     $this->translator->expectArgumentsAt(0, 'toId', array($path3 = '/about'));
     $this->translator->setReturnValueAt(0, 'toId', $oid3);
     $this->translator->expectArgumentsAt(1, 'toId', array($path2 = '/about/news'));
     $this->translator->setReturnValueAt(1, 'toId', $oid2);
     $this->translator->expectArgumentsAt(2, 'toId', array($path1));
     $this->translator->setReturnValueAt(2, 'toId', $oid1);
     $this->translator->expectCallCount('toId', 3);
     $dao = new BreadcrumbsDAO();
     $this->uow->expectArgumentsAt(0, 'load', array($class_name3, $oid3));
     $this->uow->setReturnValueAt(0, 'load', $object3);
     $this->uow->expectArgumentsAt(1, 'load', array($class_name2, $oid2));
     $this->uow->setReturnValueAt(1, 'load', $object2);
     $this->uow->expectArgumentsAt(2, 'load', array($class_name1, $oid1));
     $this->uow->setReturnValueAt(2, 'load', $object1);
     $this->uow->expectCallCount('load', 3);
     $rs =& $dao->fetch();
     $this->assertEqual($rs->getTotalRowCount(), 3);
     $rs->rewind();
     $record =& $rs->current();
     $this->assertEqual($record->get('title'), $title3);
     $this->assertEqual($record->get('_node_path'), $path3);
     $rs->next();
     $record =& $rs->current();
     $this->assertEqual($record->get('title'), $title2);
     $this->assertEqual($record->get('_node_path'), $path2);
     $rs->next();
     $record =& $rs->current();
     $this->assertEqual($record->get('title'), $title1);
     $this->assertEqual($record->get('_node_path'), $path1);
 }
  function testPreserveState()
  {
    $template = '<form id="testForm" name="testForm" runat="server">' .
                '<limb:PRESERVE_STATE name="id">' .
                '</form>';

    RegisterTestingTemplate('/limb/preserve_state1.html', $template);

    $data = new Dataspace();
    $data->set('id', 10000);

    $page =& new Template('/limb/preserve_state1.html');
    $form =& $page->findChild('testForm');
    $form->registerDatasource($data);

    $this->assertEqual($page->capture(),
                       '<form id="testForm" name="testForm">' .
                       '<input type="hidden" name="id" value="10000">' .
                       '</form>');
  }
  function testFetch()
  {
    $toolkit =& Limb :: toolkit();
    $resolver = new MockRequestResolver($this);
    $toolkit->setRequestResolver('tree_based_entity', $resolver);

    $resolver->tally();

    $entity = new Object();
    $entity->set('class_name', $class_name = 'TestArticle');

    $request =& $toolkit->getRequest();
    $resolver->setReturnReference('resolve', $entity, array($request));

    $dao = new TreeBasedEntityDAO();
    $result =& $dao->fetchRecord();
    $expected_result = new Dataspace();
    $expected_result->set('class_name', $class_name);

    $this->assertEqual($result, $expected_result);
  }
  function testTreeNodeIdRuleError()
  {
    $this->validator->addRule(new TreeNodeIdRule('test'));

    $data = new Dataspace();
    $data->set('test', -10000);

    $this->error_list->expectOnce('addError', array('test', Strings :: get('error_invalid_tree_node_id', 'error'), array()));

    $this->validator->validate($data);
    $this->assertFalse($this->validator->isValid());
  }
  function testTreeIdentifierNodeIdNotSetError()
  {
    $this->validator->addRule(new TreeIdentifierRule('test', $this->node_id_ru));

    $data = new Dataspace();
    $data->set('test', 'doc1');

    $this->error_list->expectOnce('addError', array('test', Strings :: get('error_duplicate_tree_identifier', 'error'), array()));

    $this->validator->validate($data);
    $this->assertFalse($this->validator->isValid());
  }
  function testValidSinceTheSameObject()
  {
    $this->tree->expectOnce('isNode', array($parent_node_id  = 10));
    $this->tree->setReturnValue('isNode', true);

    $this->tree->expectOnce('getChildren', array($parent_node_id  = 10));

    $rs = new ArrayDataSet(array(array('identifier' => $identifier = 'test',
                                       'id' => $node_id = 100)));
    $this->tree->setReturnReference('getChildren', $rs);

    $this->validator->addRule(new TreeIdentifierRule('test', $parent_node_id, $node_id));

    $data = new Dataspace();
    $data->set('test', $identifier);

    $this->ErrorList->expectNever('addError');

    $this->validator->validate($data);
    $this->assertTrue($this->validator->isValid());
  }
  function testValid()
  {
    $this->tree->expectOnce('getNode', array($node_id = 10));
    $this->tree->setReturnValue('getNode', array('whatever'));

    $this->validator->addRule(new TreeNodeIdRule('test'));

    $data = new Dataspace();
    $data->set('test', $node_id);

    $this->ErrorList->expectNever('addError');

    $this->validator->validate($data);
    $this->assertTrue($this->validator->isValid());
  }
  function testRefererFormSubmittedUseCurrent()
  {
    $template = "<form id='test' runat='server'><limb:form:REFERER use_current='TRUE'></form>";

    RegisterTestingTemplate('/limb/form-ref5.html', $template);

    $page =& new Template('/limb/form-ref5.html');

    $referer = 'put-me-into-result';
    $dataspace = new Dataspace();
    $dataspace->set('referer', $referer);

    $form =& $page->getChild('test');
    $form->registerDataSource($dataspace);

    $_SERVER['HTTP_REFERER'] = 'another-referer';
    $_SERVER['REQUEST_URI'] = 'another-referer';

    $result = $page->capture();
    $this->assertEqual($result,
                       "<form id=\"test\"><input type='hidden' name='referer' value='$referer'></form>");
  }
  function testEmailUserInvalidDomain()
  {
    $this->validator->addRule(new EmailRule('testfield'));

    $data = new Dataspace();
    $data->set('testfield', 'billgates@micro$oft.com');

    $this->error_list->expectOnce('addError', array('testfield', Strings :: get('bad_domain_characters', 'error'), array()));

    $this->validator->validate($data);
    $this->assertFalse($this->validator->isValid());
  }