/ */ namespace AvatarTest\Controller; use ModuleTest\TestControllerCase; use Zend\Stdlib\Parameters; use Users\Model\User; use Projects\Model\Project; use P4\Spec\Depot; use P4\Spec\Client; class IndexControllerTest extends TestControllerCase { /** * Test project add action when admin permission is enforced. */ public function testAddActionWithAdminRequired() { $services = $this->getApplication()->getServiceManager(); $config = $services->get('config'); $config['security']['add_project_admin_only'] = true; $services->setService('config', $config); $this->dispatch('/projects/add'); $this->assertRoute('projectAddAvatar'); $this->assertRouteMatch('avatar', 'avatar\controller\indexcontroller', 'projectAdd'); $this->assertResponseStatusCode(403); } /** * Test project add action with no parameters. */ public function testAddActionNoParams() { $this->dispatch('/projects/add'); $result = $this->getResult(); $this->assertRoute('projectAddAvatar'); $this->assertRouteMatch('avatar', 'avatar\controller\indexcontroller', 'projectAdd'); $this->assertResponseStatusCode(200); $this->assertInstanceOf('Zend\View\Model\ViewModel', $result); $this->assertQueryContentContains('h1', 'Add Project'); $this->assertQueryContentContains('form label', 'Name'); $this->assertQueryContentContains('form label', 'Description'); $this->assertQueryContentContains('form label', 'Owners'); $this->assertQueryContentContains('form label', 'Members'); $this->assertQueryContentContains('form label', 'Branches'); $this->assertQueryContentContains('form label', 'Automated Tests'); $this->assertQuery('form input[name="name"]'); $this->assertQuery('form textarea[name="description"]'); $this->assertQuery('form .control-group-owners input#owners'); $this->assertQuery('form .control-group-members input#members'); } public function testEditAction() { // create few users to test with and prepare their connections $p4Member = $this->connectWithAccess('foo-member', array('//...' => 'list')); $p4Owner = $this->connectWithAccess('foo-owner', array('//...' => 'list')); // create project to test with $project = new Project($this->p4); $project->set( array( 'id' => 'prj', 'members' => array('foo-member'), 'owners' => array() ) )->save(); // try to edit as non-member while owners are not enabled and verify it doesn't work $this->getApplication()->getServiceManager()->setService('p4_user', $p4Owner); $this->dispatch('/project/edit/prj'); $this->assertRoute('projectEditAvatar'); $this->assertRouteMatch('avatar', 'avatar\controller\indexcontroller', 'projectEdit'); $this->assertResponseStatusCode(403); $this->assertQueryContentContains('.error-exceptions', 'This operation is limited to project members'); // try to edit as member and verify it works $this->resetApplication(); $this->getApplication()->getServiceManager()->setService('p4_user', $p4Member); $this->dispatch('/project/edit/prj'); $this->assertRoute('projectEditAvatar'); $this->assertRouteMatch('avatar', 'avatar\controller\indexcontroller', 'projectEdit'); $this->assertResponseStatusCode(200); // add owner $project->set('owners', array('foo-owner'))->save(); // verify that owner can edit the project $this->resetApplication(); $this->getApplication()->getServiceManager()->setService('p4_user', $p4Owner); $this->dispatch('/project/edit/prj'); $this->assertRoute('projectEditAvatar'); $this->assertRouteMatch('avatar', 'avatar\controller\indexcontroller', 'projectEdit'); $this->assertResponseStatusCode(200); // if owners are enabled, non-owner/admin members can no longer edit the project $this->resetApplication(); $this->getApplication()->getServiceManager()->setService('p4_user', $p4Member); $this->dispatch('/project/edit/prj'); $this->assertRoute('projectEditAvatar'); $this->assertRouteMatch('avatar', 'avatar\controller\indexcontroller', 'projectEdit'); $this->assertResponseStatusCode(403); $this->assertQueryContentContains('.error-exceptions', 'This operation is limited to project owners'); // edit as admin when owners are enabled should also work $this->resetApplication(); $services = $this->getApplication()->getServiceManager(); $services->setService('p4_user', $services->get('p4_admin')); $this->dispatch('/project/edit/prj'); $this->assertRoute('projectEditAvatar'); $this->assertRouteMatch('avatar', 'avatar\controller\indexcontroller', 'projectEdit'); $this->assertResponseStatusCode(200); } /** * Tests the ajax upload */ public function testTempFile() { $tempFile = 'nofile'; $fileData = new Parameters( array( 'file' => array( 'name' => 'valid.jpg', 'type' => 'image/jpeg', 'tmp_name' => $tempFile, 'error' => 0, 'size' => 7275 ) ) ); $this->getRequest() ->setMethod(\Zend\Http\Request::METHOD_POST) ->setFiles($fileData); $this->dispatch('/projects/add/image/avatar'); $this->assertRoute('projectImage'); $response = json_decode($this->getResponse()->getContent()); $this->assertFalse($response->isValid); // put the test image in the temporary directory for this system $tempFile = tempnam(sys_get_temp_dir(), 'tst'); copy(__DIR__ . '/../../collateral/testAvatar.jpg', $tempFile); $fileData = new Parameters( array( 'file' => array( 'name' => 'valid.jpg', 'type' => 'image/jpeg', 'tmp_name' => $tempFile, 'error' => 0, 'size' => 7275 ) ) ); $this->getRequest() ->setMethod(\Zend\Http\Request::METHOD_POST) ->setFiles($fileData); $this->dispatch('/projects/add/image/avatar'); $this->assertRoute('projectImage'); $response = json_decode($this->getResponse()->getContent()); $this->assertTrue($response->isValid); } /** * Tests the ajax upload */ public function testSubmit() { // put the test image in the temporary directory for this system $tempFile = tempnam(sys_get_temp_dir(), 'tst'); copy(__DIR__ . '/../../collateral/testAvatar.jpg', $tempFile); $fileData = new Parameters( array( 'file' => array( 'name' => 'valid.jpg', 'type' => 'image/jpeg', 'tmp_name' => $tempFile, 'error' => 0, 'size' => 7275 ) ) ); $this->getRequest() ->setMethod(\Zend\Http\Request::METHOD_POST) ->setFiles($fileData); $this->dispatch('/projects/add/image/avatar'); $this->assertRoute('projectImage'); $response = json_decode($this->getResponse()->getContent()); $this->assertTrue($response->isValid); $tempfile = $response->filename; $this->resetApplication(); $user = new User($this->p4); $user->setId('foo') ->setEmail('foo@test.com') ->setFullName('Mr Foo') ->setPassword('abcd1234') ->save(); $projectData = array( 'name' => 'prj123', 'members' => array('foo'), 'avatar' => $tempfile ); $postData = new Parameters($projectData); $this->getRequest() ->setMethod(\Zend\Http\Request::METHOD_POST) ->setPost($postData); // dispatch and check output $this->dispatch('/projects/add'); $result = $this->getResult(); $this->assertInstanceOf('Zend\View\Model\JsonModel', $result); $this->assertRoute('projectAddAvatar'); $this->assertRouteMatch('avatar', 'avatar\controller\indexcontroller', 'projectAdd'); $this->assertResponseStatusCode(200); // if no messages, check the project was saved and is dispatchable $id = $this->userP4->getUser() . '-' . $projectData['name']; $this->assertSame(true, $result->getVariable('isValid')); $this->assertSame('/projects/' . $id, $result->getVariable('redirect')); $this->assertTrue(Project::exists($id, $this->p4)); $this->resetApplication(); $this->dispatch('/projects/' . $id); $this->assertRoute('project'); $this->assertRouteMatch('projects', 'projects\controller\indexcontroller', 'project'); $this->assertResponseStatusCode(200); } /** * Tests the avatar fetch. */ public function testFetchAvatarFail() { // create user to add in project members $user = new User; $user->setId('bar')->set('FullName', 'bar')->set('Email', 'test@host')->save(); $project = new Project($this->p4); $project->set(array('id' => 'foo', 'members' => array('bar')))->save(); $this->dispatch('/projects/foo/image/avatar/64'); $this->assertResponseStatusCode(404); } public function testFetchAvatarPass() { $pool = $this->p4->getService('clients'); $pool->grab(); $storage = $this->getApplication()->getServiceManager()->get('depot_storage'); $storage->writeFromFile('//.swarm/avatars/foovitar.jpg', __DIR__ . '/../../collateral/testAvatar.jpg'); $project = new Project($this->p4); $project->set(array('id' => 'foo', 'members' => array('bar'), 'avatar' => 'foovitar.jpg'))->save(); $this->dispatch('/projects/foo/image/avatar/64'); $json = json_decode($this->getResponse()->getBody()); $this->assertResponseStatusCode(200); $this->assertEquals($json->bits, 8); $this->assertEquals($json->channels, 3); $this->assertEquals($json->mime, "image/jpeg"); } public function setUp() { parent::setUp(); // create new depot $depot = new Depot; $depot ->setConnection($this->superP4) ->setId('.swarm') ->set( array( 'Type' => 'local', 'Map' => '.swarm/...' ) ) ->save(); $this->p4->disconnect(); $client = Client::fetch('test-client'); $client->addView('//.swarm/...', '//test-client/.swarm/...')->save(); } }