firefly-iii/tests/Api/V1/Controllers/AttachmentControllerTest.php

364 lines
14 KiB
PHP
Raw Normal View History

2018-07-01 11:31:02 -05:00
<?php
/**
* AttachmentControllerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Api\V1\Controllers;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Models\Attachment;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2018-07-01 11:31:02 -05:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2018-12-16 06:55:19 -06:00
use FireflyIII\Transformers\AttachmentTransformer;
2018-07-01 11:31:02 -05:00
use Laravel\Passport\Passport;
use Log;
use Tests\TestCase;
/**
*
* Class AttachmentControllerTest
*/
class AttachmentControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Passport::actingAs($this->user());
2019-04-09 13:05:20 -05:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-07-01 11:31:02 -05:00
}
/**
* Destroy account over API.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
*/
public function testDelete(): void
{
// mock stuff:
2018-12-10 14:45:44 -06:00
$repository = $this->mock(AttachmentRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(AttachmentTransformer::class);
2018-07-01 11:31:02 -05:00
// mock calls:
2018-12-16 06:55:19 -06:00
$repository->shouldReceive('setUser')->atLeast()->once();
2018-07-01 11:31:02 -05:00
$repository->shouldReceive('destroy')->once()->andReturn(true);
// get attachment:
$attachment = $this->user()->attachments()->first();
// call API
2018-12-16 06:55:19 -06:00
$response = $this->delete(route('api.v1.attachments.delete', [$attachment->id]));
2018-07-01 11:31:02 -05:00
$response->assertStatus(204);
}
/**
* Download attachment
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
*/
public function testDownload(): void
{
2019-04-10 23:06:25 -05:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2018-07-01 11:31:02 -05:00
// mock stuff:
2018-12-10 14:45:44 -06:00
$repository = $this->mock(AttachmentRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(AttachmentTransformer::class);
2018-12-10 14:45:44 -06:00
$content = 'Attachment content ' . random_int(100, 1000);
2018-07-01 11:31:02 -05:00
// mock calls:
2018-12-16 06:55:19 -06:00
$repository->shouldReceive('setUser')->atLeast()->once();
2018-07-01 11:31:02 -05:00
$repository->shouldReceive('exists')->andReturn(true)->once();
$repository->shouldReceive('getContent')->andReturn($content)->once();
// get attachment:
$attachment = $this->user()->attachments()->first();
// call API
2018-12-16 06:55:19 -06:00
$response = $this->get(route('api.v1.attachments.download', [$attachment->id]));
2018-07-01 11:31:02 -05:00
$response->assertStatus(200);
$response->assertSee($content);
}
/**
* Download attachment but file doesn't exist.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
*/
public function testDownloadNotExisting(): void
{
2019-04-10 23:06:25 -05:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2018-07-01 11:31:02 -05:00
// mock stuff:
2018-12-10 14:45:44 -06:00
$repository = $this->mock(AttachmentRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(AttachmentTransformer::class);
2018-12-10 14:45:44 -06:00
$content = 'Attachment content ' . random_int(100, 1000);
2018-07-01 11:31:02 -05:00
// mock calls:
2018-12-16 06:55:19 -06:00
$repository->shouldReceive('setUser')->atLeast()->once();
2018-07-01 11:31:02 -05:00
$repository->shouldReceive('exists')->andReturn(false)->once();
// get attachment:
$attachment = $this->user()->attachments()->first();
// call API
2018-12-16 06:55:19 -06:00
$response = $this->get(route('api.v1.attachments.download', [$attachment->id]));
2018-07-01 11:31:02 -05:00
$response->assertStatus(500);
$response->assertSee('Could not find the indicated attachment. The file is no longer there.');
}
/**
2018-07-01 11:58:41 -05:00
* Download attachment but no file uploaded
2018-07-01 11:31:02 -05:00
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
*/
public function testDownloadNotUploaded(): void
{
// mock stuff:
2018-12-10 14:45:44 -06:00
$repository = $this->mock(AttachmentRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(AttachmentTransformer::class);
2018-07-01 11:31:02 -05:00
// mock calls:
2018-12-16 06:55:19 -06:00
$repository->shouldReceive('setUser')->atLeast()->once();
2018-07-01 11:31:02 -05:00
// create attachment
$attachment = Attachment::create(
[
'user_id' => $this->user()->id,
'attachable_id' => 1,
'attachable_type' => TransactionJournal::class,
'md5' => md5('Hello' . random_int(1, 10000)),
2018-07-01 11:31:02 -05:00
'filename' => 'some name',
'mime' => 'text/plain',
'size' => 5,
'uploaded' => false,
]
);
// call API
2018-12-16 06:55:19 -06:00
$response = $this->get(route('api.v1.attachments.download', [$attachment->id]));
2018-07-01 11:31:02 -05:00
$response->assertStatus(500);
$response->assertSee('No file has been uploaded for this attachment (yet).');
}
/**
* List all attachments.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
*/
public function testIndex(): void
{
// create stuff
$attachments = factory(Attachment::class, 10)->create();
// mock stuff:
2018-12-10 14:45:44 -06:00
$repository = $this->mock(AttachmentRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(AttachmentTransformer::class);
2018-12-16 06:55:19 -06:00
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
2018-07-01 11:31:02 -05:00
// mock calls:
2018-12-16 06:55:19 -06:00
$repository->shouldReceive('setUser')->atLeast()->once();
2018-07-01 11:31:02 -05:00
$repository->shouldReceive('get')->once()->andReturn($attachments);
// test API
2018-12-16 06:55:19 -06:00
$response = $this->get(route('api.v1.attachments.index'));
2018-07-01 11:31:02 -05:00
$response->assertStatus(200);
$response->assertJson(['data' => [],]);
$response->assertJson(['meta' => ['pagination' => ['total' => 10, 'count' => 10, 'per_page' => true, 'current_page' => 1, 'total_pages' => 1]],]);
$response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* List one attachment.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
*/
public function testShow(): void
{
/** @var Attachment $attachment */
$attachment = $this->user()->attachments()->first();
// mock stuff:
2018-12-10 14:45:44 -06:00
$repository = $this->mock(AttachmentRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(AttachmentTransformer::class);
2018-07-01 11:31:02 -05:00
// mock calls:
2018-12-16 06:55:19 -06:00
$repository->shouldReceive('setUser')->atLeast()->once();
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
2018-07-01 11:31:02 -05:00
// test API
2018-12-16 06:55:19 -06:00
$response = $this->get(route('api.v1.attachments.show', [$attachment->id]));
2018-07-01 11:31:02 -05:00
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Store a new attachment.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
* @covers \FireflyIII\Api\V1\Requests\AttachmentRequest
2018-07-01 11:31:02 -05:00
*/
public function testStore(): void
{
/** @var Attachment $attachment */
$attachment = $this->user()->attachments()->first();
// mock stuff:
2018-12-10 14:45:44 -06:00
$repository = $this->mock(AttachmentRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(AttachmentTransformer::class);
2018-12-16 06:55:19 -06:00
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
2018-07-01 11:31:02 -05:00
// mock calls:
$journal = $this->getRandomWithdrawal();
2018-09-27 06:11:31 -05:00
$repository->shouldReceive('setUser')->atLeast()->once();
2018-07-01 11:31:02 -05:00
$repository->shouldReceive('store')->once()->andReturn($attachment);
2018-09-27 06:11:31 -05:00
$repository->shouldReceive('getNoteText')->andReturn('Hi There');
2018-07-01 11:31:02 -05:00
$journalRepos->shouldReceive('setUser')->once();
2018-12-21 09:38:10 -06:00
$journalRepos->shouldReceive('findNull')->once()->andReturn($journal);
2018-07-01 11:31:02 -05:00
// data to submit
$data = [
'filename' => 'Some new att',
'description' => sprintf('Attempt #%d', random_int(1, 10000)),
2018-12-21 09:38:10 -06:00
'model' => 'TransactionJournal',
'model_id' => $journal->id,
2018-07-01 11:31:02 -05:00
];
// test API
2018-12-21 09:38:10 -06:00
$response = $this->post(route('api.v1.attachments.store'), $data, ['accept' => 'application/json']);
2018-07-01 11:31:02 -05:00
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
2018-12-21 09:38:10 -06:00
2018-07-01 11:31:02 -05:00
}
/**
* Update an attachment.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
* @covers \FireflyIII\Api\V1\Requests\AttachmentRequest
*/
public function testUpdate(): void
{
// mock repositories
2018-12-10 14:45:44 -06:00
$repository = $this->mock(AttachmentRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(AttachmentTransformer::class);
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
2018-07-01 11:31:02 -05:00
/** @var Attachment $attachment */
$attachment = $this->user()->attachments()->first();
// mock calls:
2018-09-27 06:11:31 -05:00
$repository->shouldReceive('setUser')->atLeast()->once();
2018-07-01 11:31:02 -05:00
$repository->shouldReceive('update')->once()->andReturn($attachment);
2018-09-27 06:11:31 -05:00
$repository->shouldReceive('getNoteText')->andReturn('Hi There');
2018-07-01 11:31:02 -05:00
// data to submit
$data = [
'filename' => $attachment->filename,
'description' => sprintf('Attempt #%d', random_int(1, 10000)),
2018-12-21 09:38:10 -06:00
'model' => 'TransactionJournal',
2018-07-01 11:31:02 -05:00
'model_id' => 1,
];
// test API
2018-12-16 06:55:19 -06:00
$response = $this->put(route('api.v1.attachments.update', [$attachment->id]), $data, ['Accept' => 'application/json']);
2018-07-01 11:31:02 -05:00
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
2018-07-01 11:58:41 -05:00
/**
* Upload file for attachment.
*
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
*
*/
2018-07-01 11:31:02 -05:00
public function testUpload(): void
{
2018-12-10 14:45:44 -06:00
$repository = $this->mock(AttachmentRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(AttachmentTransformer::class);
$repository->shouldReceive('setUser')->once();
2018-07-01 11:31:02 -05:00
/** @var Attachment $attachment */
$attachment = $this->user()->attachments()->first();
2018-07-01 11:58:41 -05:00
$content = 'Hello there';
2018-07-01 11:31:02 -05:00
// mock helper:
2018-07-01 11:58:41 -05:00
$helper = $this->mock(AttachmentHelperInterface::class);
2018-07-01 11:31:02 -05:00
$helper->shouldReceive('saveAttachmentFromApi')->once();
2018-12-16 06:55:19 -06:00
$response = $this->call('POST', route('api.v1.attachments.upload', [$attachment->id]), [], [], [], [], $content);
2018-07-01 11:31:02 -05:00
//$response = $this->post('/api/v1/attachments/' . $attachment->id . '/upload',$content, ['Accept' => 'application/json']);
$response->assertStatus(204);
}
2018-07-22 13:33:17 -05:00
}