|
|
@@ -0,0 +1,421 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Tools\Model\Table;
|
|
|
+
|
|
|
+use Tools\TestSuite\TestCase;
|
|
|
+use Cake\ORM\Behavior;
|
|
|
+use Cake\ORM\Entity;
|
|
|
+use Cake\ORM\Query;
|
|
|
+use Cake\ORM\Table;
|
|
|
+use Cake\Core\Configure;
|
|
|
+use Cake\Auth\DefaultPasswordHasher;
|
|
|
+use Cake\ORM\TableRegistry;
|
|
|
+use Cake\Utility\Security;
|
|
|
+use Cake\Routing\Router;
|
|
|
+use Cake\Network\Request;
|
|
|
+use Cake\Auth\PasswordHasherFactory;
|
|
|
+use Cake\I18n\Time;
|
|
|
+
|
|
|
+class TableTest extends TestCase {
|
|
|
+
|
|
|
+ public $fixtures = array(
|
|
|
+ 'core.posts', 'core.authors',
|
|
|
+ 'plugin.tools.tools_users', 'plugin.tools.roles',
|
|
|
+ );
|
|
|
+
|
|
|
+ public $Users;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * SetUp method
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function setUp() {
|
|
|
+ parent::setUp();
|
|
|
+
|
|
|
+ Configure::write('App.namespace', 'TestApp');
|
|
|
+
|
|
|
+ $this->Users = TableRegistry::get('ToolsUsers');
|
|
|
+
|
|
|
+ $this->Posts = TableRegistry::get('Posts');
|
|
|
+ $this->Posts->belongsTo('Authors');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function tearDown() {
|
|
|
+ TableRegistry::clear();
|
|
|
+
|
|
|
+ parent::tearDown();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Check shims
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testFindFirst() {
|
|
|
+ $result = $this->Users->find('first', ['conditions' => ['name LIKE' => 'User %']]);
|
|
|
+ $this->assertEquals('User 1', $result['name']);
|
|
|
+
|
|
|
+ $result = $this->Users->find('first', ['conditions' => ['name NOT LIKE' => 'User %']]);
|
|
|
+ $this->assertNotEquals('User 1', $result['name']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Check shims
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testFindCount() {
|
|
|
+ $result = $this->Users->find('count');
|
|
|
+ $this->assertEquals(4, $result);
|
|
|
+
|
|
|
+ $result = $this->Users->find('count', ['conditions' => ['name' => 'User 1']]);
|
|
|
+ $this->assertEquals(1, $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testField() {
|
|
|
+ $result = $this->Users->field('name', ['conditions' => ['name' => 'User 1']]);
|
|
|
+ $this->assertEquals('User 1', $result);
|
|
|
+
|
|
|
+ $result = $this->Users->field('name', ['conditions' => ['name' => 'User xxx']]);
|
|
|
+ $this->assertNull($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * MyModelTest::testGetRelatedInUse()
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testGetRelatedInUse() {
|
|
|
+ $this->skipIf(true, 'TODO');
|
|
|
+ $results = $this->Posts->getRelatedInUse('Authors', 'author_id', 'list');
|
|
|
+ //die(debug($results->toArray()));
|
|
|
+ $expected = array(1 => 'mariano', 3 => 'larry');
|
|
|
+ $this->assertEquals($expected, $results->toArray());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * MyModelTest::testGetFieldInUse()
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testGetFieldInUse() {
|
|
|
+ $this->skipIf(true, 'TODO');
|
|
|
+ $this->db = ConnectionManager::getDataSource('test');
|
|
|
+ $this->skipIf(!($this->db instanceof Mysql), 'The test is only compatible with Mysql.');
|
|
|
+
|
|
|
+ $results = $this->Posts->getFieldInUse('author_id', 'list');
|
|
|
+ $expected = array(1 => 'First Post', 2 => 'Second Post');
|
|
|
+ $this->assertEquals($expected, $results);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * MyModelTest::testValidateDate()
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testValidateDate() {
|
|
|
+ $date = new Time('2010-01-22');
|
|
|
+ $res = $this->Users->validateDate($date);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ // Careful: now becomes 2010-03-01 in Cake3
|
|
|
+ // FIXME
|
|
|
+ $date = new Time('2010-02-29');
|
|
|
+ //debug($date->format(FORMAT_DB_DATETIME));
|
|
|
+ $res = $this->Users->validateDate($date);
|
|
|
+ //$this->assertFalse($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-23 11:11:11');
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-22')));
|
|
|
+ $res = $this->Users->validateDate($date, array('after' => 'after'), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-23');
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-24 11:11:11')));
|
|
|
+ $res = $this->Users->validateDate($date, array('after' => 'after'), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertFalse($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-25');
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-25')));
|
|
|
+ $res = $this->Users->validateDate($date, array('after' => 'after'), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-25');
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-25')));
|
|
|
+ $res = $this->Users->validateDate($date, array('after' => 'after', 'min' => 1), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertFalse($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-25');
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-24')));
|
|
|
+ $res = $this->Users->validateDate($date, array('after' => 'after', 'min' => 2), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertFalse($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-25');
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-24')));
|
|
|
+ $res = $this->Users->validateDate($date, array('after' => 'after', 'min' => 1), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-25');
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-24')));
|
|
|
+ $res = $this->Users->validateDate($date, array('after' => 'after', 'min' => 2), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertFalse($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-24');
|
|
|
+ $context = array('data' => array('before' => new Time('2010-02-24')));
|
|
|
+ $res = $this->Users->validateDate($date, array('before' => 'before', 'min' => 1), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertFalse($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-24');
|
|
|
+ $context = array('data' => array('before' => new Time('2010-02-25')));
|
|
|
+ $res = $this->Users->validateDate($date, array('before' => 'before', 'min' => 1), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-24');
|
|
|
+ $context = array('data' => array('before' => new Time('2010-02-25')));
|
|
|
+ $res = $this->Users->validateDate($date, array('before' => 'before', 'min' => 2), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertFalse($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-24');
|
|
|
+ $context = array('data' => array('before' => new Time('2010-02-26')));
|
|
|
+ $res = $this->Users->validateDate($date, array('before' => 'before', 'min' => 2), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * MyModelTest::testValidateDatetime()
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testValidateDatetime() {
|
|
|
+
|
|
|
+ $date = new Time('2010-01-22 11:11:11');
|
|
|
+ $res = $this->Users->validateDatetime($date);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ /*
|
|
|
+ $date = new Time('2010-01-22 11:61:11');
|
|
|
+ $res = $this->Users->validateDatetime($date);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertFalse($res);
|
|
|
+ */
|
|
|
+
|
|
|
+ //FIXME
|
|
|
+ $date = new Time('2010-02-29 11:11:11');
|
|
|
+ $res = $this->Users->validateDatetime($date);
|
|
|
+ //debug($res);
|
|
|
+ //$this->assertFalse($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $date = null;
|
|
|
+ $res = $this->Users->validateDatetime($date, array('allowEmpty' => true));
|
|
|
+ //debug($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ /*
|
|
|
+ $date = new Time => '0000-00-00 00:00:00');
|
|
|
+ $res = $this->Users->validateDatetime($date, array('allowEmpty' => true));
|
|
|
+ //debug($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+ */
|
|
|
+
|
|
|
+ $date = new Time('2010-02-23 11:11:11');
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-22 11:11:11')));
|
|
|
+ $res = $this->Users->validateDatetime($date, array('after' => 'after'), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-23 11:11:11');
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-24 11:11:11')));
|
|
|
+ $res = $this->Users->validateDatetime($date, array('after' => 'after'), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertFalse($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-23 11:11:11');
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-23 11:11:11')));
|
|
|
+ $res = $this->Users->validateDatetime($date, array('after' => 'after'), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertFalse($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-23 11:11:11');
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-23 11:11:11')));
|
|
|
+ $res = $this->Users->validateDatetime($date, array('after' => 'after', 'min' => 1), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertFalse($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-23 11:11:11');
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-23 11:11:11')));
|
|
|
+ $res = $this->Users->validateDatetime($date, array('after' => 'after', 'min' => 0), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-23 11:11:11');
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-23 11:11:10')));
|
|
|
+ $res = $this->Users->validateDatetime($date, array('after' => 'after'), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $date = new Time('2010-02-23 11:11:11');
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-23 11:11:12')));
|
|
|
+ $res = $this->Users->validateDatetime($date, array('after' => 'after'), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertFalse($res);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * MyModelTest::testValidateTime()
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testValidateTime() {
|
|
|
+
|
|
|
+ $date = '11:21:11';
|
|
|
+ $res = $this->Users->validateTime($date);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $date = '11:71:11';
|
|
|
+ $res = $this->Users->validateTime($date);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertFalse($res);
|
|
|
+
|
|
|
+ $date = '2010-02-23 11:11:11';
|
|
|
+ $context = array('data' => array('before' => new Time('2010-02-23 11:11:12')));
|
|
|
+ $res = $this->Users->validateTime($date, array('before' => 'before'), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $date = '2010-02-23 11:11:11';
|
|
|
+ $context = array('data' => array('after' => new Time('2010-02-23 11:11:12')));
|
|
|
+ $res = $this->Users->validateTime($date, array('after' => 'after'), $context);
|
|
|
+ //debug($res);
|
|
|
+ $this->assertFalse($res);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * MyModelTest::testValidateUrl()
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testValidateUrl() {
|
|
|
+
|
|
|
+ $data = 'www.dereuromark.de';
|
|
|
+ $res = $this->Users->validateUrl($data, array('allowEmpty' => true));
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $data = 'www.xxxde';
|
|
|
+ $res = $this->Users->validateUrl($data, array('allowEmpty' => true));
|
|
|
+ $this->assertFalse($res);
|
|
|
+
|
|
|
+ $data = 'www.dereuromark.de';
|
|
|
+ $res = $this->Users->validateUrl($data, array('allowEmpty' => true, 'autoComplete' => false));
|
|
|
+ $this->assertFalse($res);
|
|
|
+
|
|
|
+ $data = 'http://www.dereuromark.de';
|
|
|
+ $res = $this->Users->validateUrl($data, array('allowEmpty' => true, 'autoComplete' => false));
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $data = 'www.dereuromark.de';
|
|
|
+ $res = $this->Users->validateUrl($data, array('strict' => true));
|
|
|
+ $this->assertTrue($res); # aha
|
|
|
+
|
|
|
+ $data = 'http://www.dereuromark.de';
|
|
|
+ $res = $this->Users->validateUrl($data, array('strict' => false));
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $this->skipIf(empty($_SERVER['HTTP_HOST']), 'No HTTP_HOST');
|
|
|
+
|
|
|
+ $data = 'http://xyz.de/some/link';
|
|
|
+ $res = $this->Users->validateUrl($data, array('deep' => false, 'sameDomain' => true));
|
|
|
+ $this->assertFalse($res);
|
|
|
+
|
|
|
+ $data = '/some/link';
|
|
|
+ $res = $this->Users->validateUrl($data, array('deep' => false, 'autoComplete' => true));
|
|
|
+ $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
|
|
|
+
|
|
|
+ $data = 'http://' . $_SERVER['HTTP_HOST'] . '/some/link';
|
|
|
+ $res = $this->Users->validateUrl($data, array('deep' => false));
|
|
|
+ $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
|
|
|
+
|
|
|
+ $data = '/some/link';
|
|
|
+ $res = $this->Users->validateUrl($data, array('deep' => false, 'autoComplete' => false));
|
|
|
+ $this->assertTrue((env('REMOTE_ADDR') !== '127.0.0.1') ? !$res : $res);
|
|
|
+
|
|
|
+ //$this->skipIf(strpos($_SERVER['HTTP_HOST'], '.') === false, 'No online HTTP_HOST');
|
|
|
+
|
|
|
+ $data = '/some/link';
|
|
|
+ $res = $this->Users->validateUrl($data, array('deep' => false, 'sameDomain' => true));
|
|
|
+ $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
|
|
|
+
|
|
|
+ $data = 'https://github.com/';
|
|
|
+ $res = $this->Users->validateUrl($data, array('deep' => false));
|
|
|
+ $this->assertTrue($res);
|
|
|
+
|
|
|
+ $data = 'https://github.com/';
|
|
|
+ $res = $this->Users->validateUrl($data, array('deep' => true));
|
|
|
+ $this->assertTrue($res);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * MyModelTest::testValidateUnique()
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function _testValidateUnique() {
|
|
|
+
|
|
|
+ $this->Post->validate['title'] = array(
|
|
|
+ 'validateUnique' => array(
|
|
|
+ 'rule' => 'validateUnique',
|
|
|
+ 'message' => 'valErrRecordTitleExists'
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ $data = array(
|
|
|
+ 'title' => 'abc',
|
|
|
+ 'published' => 'N'
|
|
|
+ );
|
|
|
+ $this->Post->create($data);
|
|
|
+ $res = $this->Post->validates();
|
|
|
+ $this->assertTrue($res);
|
|
|
+ $res = $this->Post->save($res, array('validate' => false));
|
|
|
+ $this->assertTrue((bool)$res);
|
|
|
+
|
|
|
+ $this->Post->create();
|
|
|
+ $res = $this->Post->save($data);
|
|
|
+ $this->assertFalse($res);
|
|
|
+
|
|
|
+ $this->Post->validate['title'] = array(
|
|
|
+ 'validateUnique' => array(
|
|
|
+ 'rule' => array('validateUnique', array('published')),
|
|
|
+ 'message' => 'valErrRecordTitleExists'
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ $data = array(
|
|
|
+ 'title' => 'abc',
|
|
|
+ 'published' => 'Y'
|
|
|
+ );
|
|
|
+ $this->Post->create($data);
|
|
|
+ $res = $this->Post->validates();
|
|
|
+ $this->assertTrue($res);
|
|
|
+ $res = $this->Post->save($res, array('validate' => false));
|
|
|
+ $this->assertTrue((bool)$res);
|
|
|
+
|
|
|
+ $this->Post->create();
|
|
|
+ $res = $this->Post->save($data);
|
|
|
+ $this->assertFalse($res);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|