public function testCanFilterUploadAction()
{
$tmp = new ElggFile();
$tmp->owner_guid = $this->owner_guid;
$tmp->setFilename('tmp.gif');
$tmp->open('write');
$tmp->write(file_get_contents(elgg_get_config('dataroot') . '1/1/400x300.gif'));
$tmp->close();
$tmp_file = $tmp->getFilenameOnFilestore();
$upload = ['gif' => new UploadedFile($tmp_file, 'tmp.gif', 'image/gif', filesize($tmp_file), UPLOAD_ERR_OK, true)];
_elgg_services()->request->files->set('upload', $upload);
$file = new ElggFile();
$file->owner_guid = $this->owner_guid;
$uploaded_files = _elgg_services()->uploads->getUploadedFiles('upload');
$uploaded_file = array_shift($uploaded_files);
$upload_event_calls = 0;
$upload_hook_calls = 0;
_elgg_services()->events->registerHandler('upload:after', 'file', function ($event, $type, $object) use(&$upload_event_calls) {
$this->assertEquals('upload:after', $event);
$this->assertEquals('file', $type);
$this->assertInstanceOf(\ElggFile::class, $object);
$upload_event_calls++;
});
_elgg_services()->hooks->registerHandler('upload', 'file', function ($hook, $type, $return, $params) use(&$upload_hook_calls) {
$this->assertNull($return);
$this->assertEquals('upload', $hook);
$this->assertEquals('file', $type);
$this->assertInstanceOf(\ElggFile::class, $params['file']);
$this->assertInstanceOf(UploadedFile::class, $params['upload']);
$upload_hook_calls++;
return false;
});
$this->assertFalse($file->acceptUploadedFile($uploaded_file));
$this->assertEquals(0, $upload_event_calls);
$this->assertEquals(1, $upload_hook_calls);
$this->assertFalse($file->exists());
_elgg_services()->hooks->registerHandler('upload', 'file', function () use(&$upload_hook_calls) {
$upload_hook_calls++;
return true;
});
$this->assertTrue($file->acceptUploadedFile($uploaded_file));
$this->assertEquals(1, $upload_event_calls);
$this->assertEquals(3, $upload_hook_calls);
$this->assertFalse($file->exists());
}