public function testCanPrepareInputFile()
{
$file = new \ElggFile();
$file->owner_guid = 1;
$file->setFilename('600x300.jpg');
$service = $this->createService();
$service->saveIconFromElggFile($this->entity, $file);
$this->assertTrue($service->hasIcon($this->entity, 'master'));
$master = $service->getIcon($this->entity, 'master');
$size = getimagesize($master->getFilenameOnFilestore());
// original file should stay where it is
$this->assertTrue($file->exists());
// make sure we have a wide master
$this->assertTrue($size[0] > $size[1]);
$this->hooks->registerHandler('entity:icon:prepare', 'object', function ($hook, $type, $file, $params) {
// make sure we passed in documented params
if (!$params['entity'] instanceof \ElggEntity || !$params['file'] instanceof \ElggFile || !$file instanceof \ElggFile) {
return;
}
$new_source = new \ElggFile();
$new_source->owner_guid = 1;
$new_source->setFilename('300x600.jpg');
// replace with tall image
$file->owner_guid = $params['entity']->guid;
$file->setFilename('tmp/tmp.jpg');
$file->open('write');
$file->close();
copy($new_source->getFilenameOnFilestore(), $file->getFilenameOnFilestore());
return $file;
});
$this->assertTrue($this->hooks->hasHandler('entity:icon:prepare', 'object'));
$service = $this->createService();
$service->saveIconFromElggFile($this->entity, $file);
// original file should stay where it is
$this->assertTrue($file->exists());
$this->assertTrue($service->hasIcon($this->entity, 'master'));
$master = $service->getIcon($this->entity, 'master');
$size = getimagesize($master->getFilenameOnFilestore());
// was the file repaced with a tall image?
$this->assertTrue($size[0] < $size[1]);
}