Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 59 additions & 56 deletions lib/Cake/Test/Case/Console/Command/AclShellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@
App::uses('Shell', 'Console');
App::uses('AclShell', 'Console/Command');
App::uses('ComponentCollection', 'Controller');
App::uses('TestStringOutput', 'Test/Case/Console/Command');

class AclShellNoExit extends AclShell {

public array $dispatchShellArgs = [];

protected function _stop($status = 0) {}


public function dispatchShell()
{
$this->dispatchShellArgs[] = func_get_args();
}
}
/**
* AclShellTest class
*
Expand All @@ -37,6 +50,13 @@ class AclShellTest extends CakeTestCase {
*/
public $fixtures = array('core.aco', 'core.aro', 'core.aros_aco');

/**
* @var \AclShellNoExit
*/
private $Task;

private TestStringOutput $output;

/**
* setUp method
*
Expand All @@ -47,14 +67,11 @@ public function setUp(): void {
Configure::write('Acl.database', 'test');
Configure::write('Acl.classname', 'DbAcl');

$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
$in = $this->getMock('ConsoleInput', array(), array(), '', false);
$this->output = new TestStringOutput();
$this->input = new TestStringOutput();

$this->Task = new AclShellNoExit($this->output, $this->output, $this->input);

$this->Task = $this->getMock(
'AclShell',
array('in', 'out', 'hr', 'createFile', 'error', 'err', 'clear', 'dispatchShell'),
array($out, $out, $in)
);
$collection = new ComponentCollection();
$this->Task->Acl = new AclComponent($collection);
$this->Task->params['datasource'] = 'test';
Expand All @@ -77,17 +94,11 @@ public function testViewWithModelForeignKeyOutput() {
$this->Task->Acl->Aro->save();
$this->Task->args[0] = 'aro';

$this->Task->expects($this->at(0))->method('out')->with('Aro tree:');
$this->Task->expects($this->at(2))->method('out')
->with($this->stringContains('[1] ROOT'));

$this->Task->expects($this->at(4))->method('out')
->with($this->stringContains('[3] Gandalf'));

$this->Task->expects($this->at(6))->method('out')
->with($this->stringContains('[5] MyModel.2'));

$this->Task->view();

$this->assertStringContainsString('[1] ROOT', $this->output->output);
$this->assertStringContainsString('[3] Gandalf', $this->output->output);
$this->assertStringContainsString('[5] MyModel.2', $this->output->output);
}

/**
Expand All @@ -97,13 +108,12 @@ public function testViewWithModelForeignKeyOutput() {
*/
public function testViewWithArgument() {
$this->Task->args = array('aro', 'admins');

$this->Task->expects($this->at(0))->method('out')->with('Aro tree:');
$this->Task->expects($this->at(2))->method('out')->with(' [2] admins');
$this->Task->expects($this->at(3))->method('out')->with(' [3] Gandalf');
$this->Task->expects($this->at(4))->method('out')->with(' [4] Elrond');

$this->Task->view();

$this->assertStringContainsString('Aro tree:', $this->output->output);
$this->assertStringContainsString(' [2] admins', $this->output->output);
$this->assertStringContainsString(' [3] Gandalf', $this->output->output);
$this->assertStringContainsString(' [4] Elrond', $this->output->output);
}

/**
Expand All @@ -130,12 +140,11 @@ public function testParsingModelAndForeignKey() {
*/
public function testCreate() {
$this->Task->args = array('aro', 'root', 'User.1');
$this->Task->expects($this->at(0))->method('out')->with("<success>New Aro</success> 'User.1' created.", 2);
$this->Task->expects($this->at(1))->method('out')->with("<success>New Aro</success> 'User.3' created.", 2);
$this->Task->expects($this->at(2))->method('out')->with("<success>New Aro</success> 'somealias' created.", 2);

$this->Task->create();

$this->assertStringContainsString($this->output->styleText("<success>New Aro</success> 'User.1' created."), $this->output->output);

$Aro = ClassRegistry::init('Aro');
$Aro->cacheQueries = false;
$result = $Aro->read();
Expand All @@ -147,6 +156,8 @@ public function testCreate() {
$this->Task->args = array('aro', 'User.1', 'User.3');
$this->Task->create();

$this->assertStringContainsString($this->output->styleText("<success>New Aro</success> 'User.3' created."), $this->output->output);

$Aro = ClassRegistry::init('Aro');
$result = $Aro->read();
$this->assertEquals('User', $result['Aro']['model']);
Expand All @@ -156,6 +167,8 @@ public function testCreate() {
$this->Task->args = array('aro', 'root', 'somealias');
$this->Task->create();

$this->assertStringContainsString($this->output->styleText("<success>New Aro</success> 'somealias' created."), $this->output->output);

$Aro = ClassRegistry::init('Aro');
$result = $Aro->read();
$this->assertEquals('somealias', $result['Aro']['alias']);
Expand All @@ -171,9 +184,8 @@ public function testCreate() {
*/
public function testDelete() {
$this->Task->args = array('aro', 'AuthUser.1');
$this->Task->expects($this->at(0))->method('out')
->with("<success>Aro deleted.</success>", 2);
$this->Task->delete();
$this->assertStringContainsString($this->output->styleText("<success>Aro deleted.</success>"), $this->output->output);

$Aro = ClassRegistry::init('Aro');
$result = $Aro->findById(3);
Expand Down Expand Up @@ -201,9 +213,9 @@ public function testSetParent() {
*/
public function testGrant() {
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
$this->Task->expects($this->at(0))->method('out')
->with($this->matchesRegularExpression('/granted/'), true);
$this->Task->grant();

$this->assertStringContainsString('granted', $this->output->output);
$node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
$node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);

Expand All @@ -218,11 +230,10 @@ public function testGrant() {
*/
public function testDeny() {
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
$this->Task->expects($this->at(0))->method('out')
->with($this->stringContains('Permission denied'), true);

$this->Task->deny();

$this->assertStringContainsString('Permission denied', $this->output->output);

$node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
$node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);
$this->assertFalse(empty($node['Aco'][0]));
Expand All @@ -235,26 +246,25 @@ public function testDeny() {
* @return void
*/
public function testCheck() {
$this->Task->expects($this->at(0))->method('out')
->with($this->matchesRegularExpression('/not allowed/'), true);
$this->Task->expects($this->at(1))->method('out')
->with($this->matchesRegularExpression('/granted/'), true);
$this->Task->expects($this->at(2))->method('out')
->with($this->matchesRegularExpression('/is.*allowed/'), true);
$this->Task->expects($this->at(3))->method('out')
->with($this->matchesRegularExpression('/not.*allowed/'), true);

$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', '*');
$this->Task->check();
$this->assertStringContainsString('not allowed', $this->output->output);

$this->output->output = '';
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
$this->Task->grant();
$this->assertStringContainsString('granted', $this->output->output);

$this->output->output = '';
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
$this->Task->check();
$this->assertMatchesRegularExpression('/is.*allowed/', $this->output->output);

$this->output->output = '';
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'delete');
$this->Task->check();
$this->assertMatchesRegularExpression('/not.*allowed/', $this->output->output);
}

/**
Expand All @@ -263,16 +273,15 @@ public function testCheck() {
* @return void
*/
public function testInherit() {
$this->Task->expects($this->at(0))->method('out')
->with($this->matchesRegularExpression('/Permission .*granted/'), true);
$this->Task->expects($this->at(1))->method('out')
->with($this->matchesRegularExpression('/Permission .*inherited/'), true);

$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
$this->Task->grant();
$this->assertMatchesRegularExpression('/Permission .*granted/', $this->output->output);

$this->output->output = '';
$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'all');
$this->Task->inherit();
$this->assertMatchesRegularExpression('/Permission .*inherited/', $this->output->output);

$node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
$node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);
Expand All @@ -291,21 +300,15 @@ public function testGetPath() {
$first = $node[0]['Aro']['id'];
$second = $node[1]['Aro']['id'];
$last = $node[2]['Aro']['id'];
$this->Task->expects($this->at(2))->method('out')->with('[' . $last . '] ROOT');
$this->Task->expects($this->at(3))->method('out')->with(' [' . $second . '] admins');
$this->Task->expects($this->at(4))->method('out')->with(' [' . $first . '] Elrond');
$this->Task->getPath();
$linhas = explode("\n", $this->output->output);
$this->assertEquals('[' . $last . '] ROOT', $linhas[2]);
$this->assertEquals(' [' . $second . '] admins', $linhas[3]);
$this->assertEquals(' [' . $first . '] Elrond', $linhas[4]);
}

/**
* test that initdb makes the correct call.
*
* @return void
*/
public function testInitDb() {
$this->Task->expects($this->once())->method('dispatchShell')
->with('schema create DbAcl');

$this->Task->initdb();
$this->assertEquals('schema create DbAcl', $this->Task->dispatchShellArgs[0][0]);
}
}
38 changes: 10 additions & 28 deletions lib/Cake/Test/Case/Console/Command/CommandListShellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,7 @@
App::uses('ConsoleInput', 'Console');
App::uses('Shell', 'Console');
App::uses('CommandTask', 'Console/Command/Task');

/**
* TestStringOutput
*
* @package Cake.Test.Case.Console.Command
*/
class TestStringOutput extends ConsoleOutput {

public $output = '';

protected function _write($message) {
$this->output .= $message;
}

}
App::uses('TestStringOutput', 'Test/Case/Console/Command');

/**
* CommandListShellTest
Expand All @@ -62,19 +48,15 @@ public function setUp(): void {
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));

$out = new TestStringOutput();
$in = $this->getMock('ConsoleInput', array(), array(), '', false);

$this->Shell = $this->getMock(
'CommandListShell',
array('in', '_stop', 'clear'),
array($out, $out, $in)
);

$this->Shell->Command = $this->getMock(
'CommandTask',
array('in', '_stop', 'clear'),
array($out, $out, $in)
);
$in = $this->getMockBuilder('ConsoleInput')->getMock();

$this->Shell = $this->getMockBuilder('CommandListShell')
->onlyMethods(array('in', '_stop', 'clear'))->setConstructorArgs(array($out, $out, $in))
->getMock();

$this->Shell->Command = $this->getMockBuilder('CommandTask')
->onlyMethods(array('in', '_stop', 'clear'))
->setConstructorArgs(array($out, $out, $in))->getMock();
}

/**
Expand Down
14 changes: 14 additions & 0 deletions lib/Cake/Test/Case/Console/Command/TestStringOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

class TestStringOutput extends ConsoleOutput
{

/** @var string */
public $output = '';

protected function _write($message)
{
$this->output .= $message;
}

}
4 changes: 3 additions & 1 deletion lib/Cake/Test/Case/Network/CakeResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ public function testHeader() {
* @return void
*/
public function testSend() {
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent', '_setCookies'));
$response = $this->getMockBuilder('CakeResponse')
->onlyMethods(array('_sendHeader', '_sendContent', '_setCookies'))
->getMock();
$response->header(array(
'Content-Language' => 'es',
'WWW-Authenticate' => 'Negotiate',
Expand Down