/** * @dataProvider GetTestGetTransparentAuthenticationCredentials */ public function testGetTransparentAuthenticationCredentials($inputData, $serverGlobals, $shouldSucceed) { $input = static::$container->input; // Clear input data ReflectionHelper::setValue($input, 'data', array()); // Push input data if so defined if (!is_null($inputData)) { ReflectionHelper::setValue($input, 'data', $inputData); } // Override server globals if necessary if (!is_null($serverGlobals)) { foreach ($serverGlobals as $k => $v) { $_SERVER[$k] = $v; } } // Decode the transparent authentication information $result = $this->auth->getTransparentAuthenticationCredentials(); if ($shouldSucceed) { $this->assertInternalType('array', $result); $this->assertArrayHasKey('username', $result); $this->assertArrayHasKey('password', $result); $this->assertEquals('FOF30test', $result['username']); $this->assertEquals('dummy', $result['password']); } else { $this->assertNull($result); } }
/** * @covers FOF30\Database\Installer::__construct * @covers FOF30\Database\Installer::updateSchema * @covers FOF30\Database\Installer::findSchemaXml * @covers FOF30\Database\Installer::openAndVerify * @covers FOF30\Database\Installer::conditionMet * * @throws \Exception */ public function testCreateNewTable() { $db = static::$container->db; $tables = $db->setQuery('SHOW TABLES')->loadColumn(); $prefix = $db->getPrefix(); $this->assertFalse(in_array($prefix . 'foobar_example', $tables), 'The table must not exist before testing starts'); $installer = new Installer($db, __DIR__ . '/../_data/installer/pick_right'); ReflectionHelper::setValue($installer, 'allTables', array()); $installer->updateSchema(); $tables = $db->setQuery('SHOW TABLES')->loadColumn(); $prefix = $db->getPrefix(); $this->assertTrue(in_array($prefix . 'foobar_example', $tables), 'The table must exist after running updateSchema'); }
/** * @group Relation * @group RelationGetforeignKeyMap * @covers FOF30\Model\DataModel\Relation::getForeignKeyMap */ public function testGetForeignKeyMap() { $model = $this->buildModel(); $relation = new RelationStub($model, 'Children'); $keymap = array(1, 2, 3); ReflectionHelper::setValue($relation, 'foreignKeyMap', $keymap); $result = $relation->getForeignKeyMap(); $this->assertEquals($keymap, $result, 'Relation::getForeignKeyMap Returned the wrong result'); }
/** * @group TreeModelGetRoot * @group TreeModel * @covers FOF30\Model\TreeModel::getRoot * @dataProvider TreeModelDataprovider::getTestRoot */ public function testGetRoot($test, $check) { $config = array('autoChecks' => false, 'idFieldName' => 'foftest_nestedset_id', 'tableName' => '#__foftest_nestedsets'); $table = new TreeModelStub(static::$container, $config); // Am I request to create a different root? if ($test['newRoot']) { $root = $table->getClone(); $root->title = 'New root'; $root->insertAsRoot(); $child = $table->getClone(); $child->title = 'First child 2nd root'; $child->insertAsChildOf($root); $child->reset(); $child->title = 'Second child 2nd root'; $child->insertAsChildOf($root); $grandson = $child->getClone(); $grandson->reset(); $grandson->title = 'First grandson of second child'; $grandson->insertAsChildOf($child); } $table->findOrFail($test['loadid']); if (!is_null($test['cache'])) { if ($test['cache'] == 'loadself') { ReflectionHelper::setValue($table, 'treeRoot', $table); } else { ReflectionHelper::setValue($table, 'treeRoot', $test['cache']); } } // I have to check the lft value, since the id could change throught test iteration (table deleted and not truncated) $return = $table->getRoot(); $root = $return->lft; $this->assertEquals($check['result'], $root, 'TreeModel::getRoot returned the wrong root - Case: ' . $check['case']); }
/** * @covers FOF30\View\View::getDoTask */ public function testGetDoTask() { $view = new ViewStub(static::$container); ReflectionHelper::setValue($view, 'doTask', 'foobar'); $this->assertEquals('foobar', $view->getDoTask(), 'View::getDoTask Failed to return the doTask'); }
/** * @group DataModel * @group DataModelGetTouches * @covers FOF30\Model\DataModel::getTouches */ public function testGetTouches() { $config = array('idFieldName' => 'foftest_bare_id', 'tableName' => '#__foftest_bares'); $model = new DataModelStub(static::$container, $config); $touches = array('foo', 'bar'); ReflectionHelper::setValue($model, 'touches', $touches); $obj = $model->getTouches(); $this->assertSame($touches, $obj, 'DataModel::getTouches failed to return the internal array'); }
/** * @group RelationManager * @group RelationManagerIsMagicProperty * @covers FOF30\Model\DataModel\RelationManager::isMagicProperty * @dataProvider RelationManagerDataprovider::getTestIsMagicProperty */ public function testIsMagicProperty($test, $check) { $msg = 'RelationManager::isMagicProperty %s - Case: ' . $check['case']; $model = $this->buildModel(); $relation = new RelationManager($model); ReflectionHelper::setValue($relation, 'relations', array('foobar' => '')); $result = $relation->isMagicProperty($test['name']); $this->assertEquals($check['result'], $result, sprintf($msg, 'Failed to return the corret result')); }
/** * @group DataModel * @group DataModelUnpublish * @covers FOF30\Model\DataModel::unpublish * @dataProvider PublishDataprovider::getTestUnpublish */ public function testUnpublish($test, $check) { $before = 0; $after = 0; $msg = 'DataModel::unpublish %s - Case: ' . $check['case']; $config = array('idFieldName' => $test['tableid'], 'tableName' => $test['table']); // I am passing those methods so I can double check if the method is really called $methods = array('onBeforeUnpublish' => function () use(&$before) { $before++; }, 'onAfterUnpublish' => function () use(&$after) { $after++; }); $model = $this->getMock('\\FOF30\\Tests\\Stubs\\Model\\DataModelStub', array('save', 'getId'), array(static::$container, $config, $methods)); $model->expects($this->any())->method('getId')->willReturn(1); // Let's mock the dispatcher, too. So I can check if events are really triggered $dispatcher = $this->getMock('\\FOF30\\Event\\Dispatcher', array('trigger'), array(static::$container)); $dispatcher->expects($this->exactly($check['dispatcher']))->method('trigger')->withConsecutive(array($this->equalTo('onBeforeUnpublish')), array($this->equalTo('onAfterUnpublish'))); ReflectionHelper::setValue($model, 'behavioursDispatcher', $dispatcher); $result = $model->unpublish(); $enabled = $model->getFieldValue('enabled'); $this->assertInstanceOf('\\FOF30\\Model\\DataModel', $result, sprintf($msg, 'Should return an instance of itself')); $this->assertEquals($check['before'], $before, sprintf($msg, 'Failed to call the onBefore method')); $this->assertEquals($check['after'], $after, sprintf($msg, 'Failed to call the onAfter method')); $this->assertSame($check['enabled'], $enabled, sprintf($msg, 'Failed to set the enabled field')); }
/** * @covers FOF30\Controller\DataController::getModel * @dataProvider DataControllerDataprovider::getTestGetModel */ public function testGetModel($test, $check) { $container = new TestContainer(array('componentName' => 'com_fakeapp')); $config = array('idFieldName' => 'foftest_foobar_id', 'tableName' => '#__foftest_foobars'); $controller = new DataControllerStub($container); ReflectionHelper::setValue($controller, 'modelName', $test['mock']['modelname']); if ($check['exception']) { $this->setExpectedException('FOF30\\Controller\\Exception\\NotADataModel'); } $model = $controller->getModel($test['name'], $config); $this->assertInstanceOf('\\FOF30\\Model\\DataModel', $model, 'DataController::getModel should return a DataModel'); }
/** * @group Controller * @group ControllerSetRedirect * @covers FOF30\Controller\Controller::setRedirect * @dataProvider ControllerDataprovider::getTestSetRedirect */ public function testSetRedirect($test, $check) { $msg = 'Controller::setRedirect %s - Case: ' . $check['case']; $controller = new ControllerStub(new TestContainer(array('componentName' => 'com_eastwood'))); ReflectionHelper::setValue($controller, 'messageType', $test['mock']['type']); $result = $controller->setRedirect($test['url'], $test['msg'], $test['type']); $redirect = ReflectionHelper::getValue($controller, 'redirect'); $message = ReflectionHelper::getValue($controller, 'message'); $type = ReflectionHelper::getValue($controller, 'messageType'); $this->assertInstanceOf('\\FOF30\\Controller\\Controller', $result, sprintf($msg, 'Should return an instance of itself')); $this->assertEquals($check['redirect'], $redirect, sprintf($msg, 'Did not set the redirect url correctly')); $this->assertEquals($check['message'], $message, sprintf($msg, 'Did not set the message correctly')); $this->assertEquals($check['type'], $type, sprintf($msg, 'Did not set the message type correctly')); }
protected function tearDown() { ReflectionHelper::setValue($this->object, 'instances', array()); }
/** * @group DataModel * @group DataModel__set * @covers FOF30\Model\DataModel::__set * @dataProvider MagicMethodsDataprovider::getTest__set */ public function test__set($test, $check) { $msg = 'DataModel::__set %s - Case: ' . $check['case']; $config = array('idFieldName' => 'foftest_foobar_id', 'tableName' => '#__foftest_foobars'); $model = $this->getMock('\\FOF30\\Tests\\Stubs\\Model\\DataModelStub', array('setFieldValue', 'setState', '__call'), array(static::$container, $config)); $model->expects($check['call'] ? $this->once() : $this->never())->method('__call')->willReturn(null); $model->expects($check['setField'] ? $this->once() : $this->never())->method('setFieldValue')->with($this->equalTo($check['setField']))->willReturn(null); $model->expects($check['setState'] ? $this->once() : $this->never())->method('setState')->with($this->equalTo($check['setState']))->willReturn(null); ReflectionHelper::setValue($model, 'aliasFields', $test['mock']['alias']); $property = $test['property']; $model->{$property} = $test['value']; $count = isset($model->methodCounter[$check['method']]) ? $model->methodCounter[$check['method']] : 0; $this->assertEquals($check['count'], $count, sprintf($msg, 'Invoked the specific setter method a wrong amount of times')); }
/** * @covers FOF30\Hal\Document::addData */ public function testAddData_fromScratch() { $data = array('test1' => 'one', 'test2' => 'two', 'testArray' => array('testUno' => 'uno', 'testDue' => 'Due')); $document = new Document($data); $myLink = new Link('http://www.example.com/link1.json', false, 'test', null, 'A test link'); $document->addLink('foo', $myLink); // Make sure trying to add links with replace=false adds, doesn't replace, links $myOtherLink = new Link('http://www.example.com/otherLink.json', false, 'test', null, 'Another test link'); $document->addLink('foo', $myOtherLink, false); $myLinks = array(new Link('http://www.example.com/foo.json', false, 'foobar1'), new Link('http://www.example.com/bar.json', false, 'foobar2')); $document->addLinks('foo', $myLinks, false); ReflectionHelper::setValue($document, '_data', null); $extraData = array('newData' => 'something'); $document->addData($extraData, true); $data = $this->getObjectAttribute($document, '_data'); $this->assertInternalType('object', $data, 'Line: ' . __LINE__ . '.'); $this->assertEquals((object) $extraData, $data, 'Line: ' . __LINE__ . '.'); }
/** * @covers FOF30\Hal\Render\Json::render */ public function testRender() { $renderer = new Json($this->document); // Full render $expected = '{"_links":{"prev":{"href":"http:\\/\\/www.example.com\\/test.json?page=1"},"next":{"href":"http:\\/\\/www.example.com\\/test.json?page=3"}},"_embedded":{"detail":["{\\"_links\\":{},\\"_list\\":{\\"detail1_1\\":\\"val1_1\\",\\"detail1_2\\":\\"val1_2\\"}}","{\\"_links\\":{},\\"_list\\":{\\"detail2_1\\":\\"val2_1\\",\\"detail2_2\\":\\"val2_2\\"}}"]},"_list":{"key1":"val1","key2":"val2"}}'; $rendered = $renderer->render(); $this->assertEquals($expected, $rendered, 'Line: ' . __LINE__ . '.'); // Without embedded documents $document = $this->getObjectAttribute($renderer, '_document'); ReflectionHelper::setValue($document, '_embedded', null); ReflectionHelper::setValue($renderer, '_document', $document); $expected = '{"_links":{"prev":{"href":"http:\\/\\/www.example.com\\/test.json?page=1"},"next":{"href":"http:\\/\\/www.example.com\\/test.json?page=3"}},"_list":{"key1":"val1","key2":"val2"}}'; $rendered = $renderer->render(); $this->assertEquals($expected, $rendered, 'Line: ' . __LINE__ . '.'); // With list of links $foo = new Link('http://www.example.com/foo.json?arg=1', false); $document->addLink('foo', $foo); $foo = new Link('http://www.example.com/foo.json?arg=1', false); $document->addLink('foo', $foo); ReflectionHelper::setValue($renderer, '_document', $document); $expected = '{"_links":{"prev":{"href":"http:\\/\\/www.example.com\\/test.json?page=1"},"next":{"href":"http:\\/\\/www.example.com\\/test.json?page=3"},"foo":{"href":"http:\\/\\/www.example.com\\/foo.json?arg=1"}},"_list":{"key1":"val1","key2":"val2"}}'; $rendered = $renderer->render(); $this->assertEquals($expected, $rendered, 'Line: ' . __LINE__ . '.'); }
/** * @group DataModel * @group DataModelBlacklistFilters * @covers FOF30\Model\DataModel::blacklistFilters * @dataProvider DataModelGenericDataprovider::getTestBlacklistFilters */ public function testBlacklistFilters($test, $check) { $msg = 'DataModel::blacklistFilters %s - Case: ' . $check['case']; $config = array('idFieldName' => 'foftest_bare_id', 'tableName' => '#__foftest_bares'); $model = new DataModelStub(static::$container, $config); ReflectionHelper::setValue($model, '_behaviorParams', array('blacklistFilters' => array('test'))); $result = $model->blacklistFilters($test['list'], $test['reset']); $behaviors = ReflectionHelper::getValue($model, '_behaviorParams'); $filters = isset($behaviors['blacklistFilters']) ? $behaviors['blacklistFilters'] : array(); $this->assertSame($check['result'], $result, sprintf($msg, 'Returned the wrong result')); $this->assertEquals($check['filters'], $filters, sprintf($msg, 'Failed to set the filters')); }
/** * @group Model * @group ModelGetIgnoreRequest * @covers FOF30\Model\Model::getIgnoreRequest */ public function testGetIgnoreRequest() { $model = new ModelStub(static::$container); ReflectionHelper::setValue($model, '_ignoreRequest', 'foobar'); $result = $model->getIgnoreRequest(); $this->assertEquals('foobar', $result, 'Model::getIgnoreRequest returned the wrong value'); }
/** * @group BelongsToMany * @group BelongsToManySaveAll * @covers FOF30\Model\DataModel\Relation\BelongsToMany::saveAll */ public function testSaveAll() { $platform = static::$container->platform; $platform::$user = (object) array('id' => 42); $model = new Groups(static::$container); $model->find(1); $relation = new BelongsToMany($model, 'Parts'); $items = array(); // Let's mix datamodels with integers $items[0] = new Parts(static::$container); $items[0]->find(1); $items[0]->description = 'Modified'; for ($i = 1; $i <= 55; $i++) { $items[] = $i; } $data = new Collection($items); ReflectionHelper::setValue($relation, 'data', $data); $relation->saveAll(); $db = \JFactory::getDbo(); // First of all double check if the part was updated $query = $db->getQuery(true)->select($db->qn('description'))->from($db->qn('#__fakeapp_parts'))->where($db->qn('fakeapp_part_id') . ' = ' . $db->q(1)); $descr = $db->setQuery($query)->loadResult(); $this->assertEquals('Modified', $descr, 'BelongsToMany::saveAll Failed to save item in the relationship'); // Then let's check if all the items were saved in the glue table $query = $db->getQuery(true)->select('COUNT(*)')->from($db->qn('#__fakeapp_parts_groups'))->where($db->qn('fakeapp_group_id') . ' = ' . $db->q(1)); $count = $db->setQuery($query)->loadResult(); $this->assertEquals(55, $count, 'BelongsToMany::saveAll Failed to save data inside the glue table'); }
/** * @group BasicFactory * @covers FOF30\Factory\BasicFactory::isSaveScaffolding */ public function testIsSaveScaffolding() { $factory = new BasicFactory(static::$container); ReflectionHelper::setValue($factory, 'saveScaffolding', true); $this->assertTrue($factory->isSaveScaffolding(), 'BasicFactory::isSaveScaffolding Failed to set the save scaffolding flag'); }
/** * @covers FOF30\Toolbar\Toolbar::isDataView * @dataProvider ToolbarDataprovider::getTestIsDataView */ public function testIsDataView($test, $check) { $msg = 'Toolbar::isDataView %s - Case: ' . $check['case']; $platform = static::$container->platform; $platform::$template = 'fake_test_template'; $platform::$uriBase = 'www.example.com'; $TestContainer = static::$container; $controller = $this->getMock('\\FOF30\\Tests\\Stubs\\Controller\\ControllerStub', array('getView'), array(static::$container)); $controller->expects($this->any())->method('getView')->willReturnCallback(function () use($test, $TestContainer) { if (!is_null($test['mock']['getView'])) { return new $test['mock']['getView']($TestContainer); } return null; }); $dispacher = $this->getMock('FOF30\\Dispatcher\\Dispatcher', array('getController'), array(static::$container)); $dispacher->expects($this->any())->method('getController')->willReturn($test['mock']['getController'] ? $controller : null); $container = new TestContainer(array('dispatcher' => $dispacher)); $toolbar = new ToolbarStub($container); ReflectionHelper::setValue($toolbar, 'isDataView', $test['mock']['cache']); $result = $toolbar->isDataView(); $this->assertEquals($check['result'], $result, sprintf($msg, 'Returned the wrong result')); }
/** * @covers FOF30\Platform\Joomla\Platform::getTemplateOverridePath * * @dataProvider FOF30\Tests\Platform\PlatformJoomlaProvider::getTestGetTemplateOverridePath * */ public function testGetTemplateOverridePath($applicationType, $component, $absolute, $expected, $message) { $this->forceApplicationTypeAndResetPlatformCliAdminCache($applicationType); if ($applicationType != 'cli') { $app = \JFactory::getApplication(); $fakeTemplate = (object) array('template' => 'system'); ReflectionHelper::setValue($app, 'template', $fakeTemplate); } $actual = $this->platform->getTemplateOverridePath($component, $absolute); $this->assertEquals($expected, $actual, $message); }
/** * @group DataViewRaw * @covers FOF30\View\DataView\Raw::getPageParams */ public function testGetPageParams() { $platform = static::$container->platform; $platform::$uriBase = 'www.example.com'; $platform::$template = 'fake_test_template'; $view = new RawStub(static::$container); $value = (object) array('test' => 1, 'dummy' => 'test'); ReflectionHelper::setValue($view, 'pageParams', $value); $this->assertSame($value, $view->getPageParams(), 'Raw::getPageParams Failed to return the internal item'); }
/** * @group DataModel * @group DataModelForceDelete * @covers FOF30\Model\DataModel::forceDelete * @dataProvider DataModelCrudDataprovider::getTestForceDelete */ public function testForceDelete($test, $check) { $before = 0; $after = 0; $msg = 'DataModel::forceDelete %s - Case: ' . $check['case']; $config = array('idFieldName' => 'foftest_bare_id', 'tableName' => '#__foftest_bares'); // I am passing those methods so I can double check if the method is really called $methods = array('onBeforeDelete' => function () use(&$before) { $before++; }, 'onAfterDelete' => function () use(&$after) { $after++; }); $model = $this->getMock('\\FOF30\\Tests\\Stubs\\Model\\DataModelStub', array('getId', 'findOrFail', 'reset'), array(static::$container, $config, $methods)); $model->expects($this->once())->method('reset')->willReturn(null); $model->expects($this->any())->method('getId')->willReturn($test['mock']['id']); $model->expects($check['find'] ? $this->once() : $this->never())->method('findOrFail')->willReturn(null); // Let's mock the dispatcher, too. So I can check if events are really triggered $dispatcher = $this->getMock('\\FOF30\\Event\\Dispatcher', array('trigger'), array(static::$container)); $dispatcher->expects($this->exactly(2))->method('trigger')->withConsecutive(array($this->equalTo('onBeforeDelete')), array($this->equalTo('onAfterDelete'))); ReflectionHelper::setValue($model, 'behavioursDispatcher', $dispatcher); $result = $model->delete($test['id']); $this->assertInstanceOf('\\FOF30\\Model\\DataModel', $result, sprintf($msg, 'Should return an instance of itself')); $this->assertEquals(1, $before, sprintf($msg, 'Failed to call the onBefore method')); $this->assertEquals(1, $after, sprintf($msg, 'Failed to call the onAfter method')); // Now let's check if the record was really deleted $db = \JFactory::getDbo(); $query = $db->getQuery(true)->select('COUNT(*)')->from($db->qn('#__foftest_bares'))->where($db->qn('foftest_bare_id') . ' = ' . $db->q($check['id'])); $count = $db->setQuery($query)->loadResult(); $this->assertEquals(0, $count, sprintf($msg, 'Failed to actually delete the record in the database')); }
/** * @group DataModel * @group DataModelUnlock * @covers FOF30\Model\DataModel::unlock * @dataProvider SpecialColumnsDataprovider::getTestUnlock */ public function testUnlock($test, $check) { $before = 0; $after = 0; $db = \JFactory::getDbo(); $msg = 'DataModel::unlock %s - Case: ' . $check['case']; $config = array('idFieldName' => $test['tableid'], 'tableName' => $test['table']); // I am passing those methods so I can double check if the method is really called $methods = array('onBeforeUnlock' => function () use(&$before) { $before++; }, 'onAfterUnlock' => function () use(&$after) { $after++; }); $model = $this->getMock('\\FOF30\\Tests\\Stubs\\Model\\DataModelStub', array('save', 'getId'), array(static::$container, $config, $methods)); $model->expects($this->any())->method('getId')->willReturn(1); // Let's mock the dispatcher, too. So I can check if events are really triggered $dispatcher = $this->getMock('\\FOF30\\Event\\Dispatcher', array('trigger'), array(static::$container)); $dispatcher->expects($this->exactly($check['dispatcher']))->method('trigger')->withConsecutive(array($this->equalTo('onBeforeUnlock')), array($this->equalTo('onAfterUnlock'))); ReflectionHelper::setValue($model, 'behavioursDispatcher', $dispatcher); if ($model->hasField('locked_on')) { $now = new \JDate(); $model->setFieldValue('locked_on', $now->toSql()); } $result = $model->unlock(); $locked_by = $model->getFieldValue('locked_by'); $locked_on = $model->getFieldValue('locked_on'); $this->assertInstanceOf('\\FOF30\\Model\\DataModel', $result, sprintf($msg, 'Should return an instance of itself')); $this->assertEquals($check['before'], $before, sprintf($msg, 'Failed to call the onBefore method')); $this->assertEquals($check['after'], $after, sprintf($msg, 'Failed to call the onAfter method')); $this->assertEquals($check['locked_by'], $locked_by, sprintf($msg, 'Failed to set the locking user')); // The time is calculated on the fly, so I can only check if it's null or not if ($check['locked_on']) { $this->assertEquals($db->getNullDate(), $locked_on, sprintf($msg, 'Failed to set the locking time')); } else { $this->assertNull($locked_on, sprintf($msg, 'Failed to set the locking time')); } }