Skip to content

Commit

Permalink
! attachment test
Browse files Browse the repository at this point in the history
  • Loading branch information
Spuds committed Jan 26, 2024
1 parent 7441c87 commit 529fc5b
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 5 deletions.
11 changes: 6 additions & 5 deletions sources/subs/Attachments.subs.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use ElkArte\Cache\Cache;
use ElkArte\Errors\AttachmentErrorContext;
use ElkArte\Errors\Errors;
use ElkArte\Graphics\Image;
use ElkArte\Http\FsockFetchWebdata;
use ElkArte\TemporaryAttachment;
Expand Down Expand Up @@ -64,7 +65,7 @@ function processAttachments($id_msg = 0)
if (!$file_functions->isDir($attach_current_dir))
{
$tmp_attachments->setSystemError('attach_folder_warning');
\ElkArte\Errors\Errors::instance()->log_error(sprintf($txt['attach_folder_admin_warning'], $attach_current_dir), 'critical');
Errors::instance()->log_error(sprintf($txt['attach_folder_admin_warning'], $attach_current_dir), 'critical');

Check warning on line 68 in sources/subs/Attachments.subs.php

View check run for this annotation

Codecov / codecov/patch

sources/subs/Attachments.subs.php#L68

Added line #L68 was not covered by tests
}

if ($tmp_attachments->hasSystemError() === false && !isset($context['attachments']['quantity']))
Expand Down Expand Up @@ -190,7 +191,7 @@ function processAttachments($id_msg = 0)
$attach_errors->addError(isset($error[1]) ? $error : $error[0]);
if (in_array($error[0], $log_these))
{
\ElkArte\Errors\Errors::instance()->log_error($temp_file->getName() . ': ' . $txt[$error[0]], 'critical');
Errors::instance()->log_error($temp_file->getName() . ': ' . $txt[$error[0]], 'critical');

Check warning on line 194 in sources/subs/Attachments.subs.php

View check run for this annotation

Codecov / codecov/patch

sources/subs/Attachments.subs.php#L194

Added line #L194 was not covered by tests

// For critical errors, we don't want the file or session data to persist
$temp_file->remove(false);
Expand Down Expand Up @@ -257,18 +258,18 @@ function attachmentUploadChecks($attachID)
// 3 partially uploaded
// 4 no file uploaded
// 8 upload blocked by extension
\ElkArte\Errors\Errors::instance()->log_error($_FILES['attachment']['name'][$attachID] . ': ' . $txt['php_upload_error_' . $_FILES['attachment']['error'][$attachID]]);
Errors::instance()->log_error($_FILES['attachment']['name'][$attachID] . ': ' . $txt['php_upload_error_' . $_FILES['attachment']['error'][$attachID]]);

Check warning on line 261 in sources/subs/Attachments.subs.php

View check run for this annotation

Codecov / codecov/patch

sources/subs/Attachments.subs.php#L261

Added line #L261 was not covered by tests
$errors[] = 'attach_php_error';
break;
case 6:
case 7:
// 6 Missing or a full a temp directory on the server
// 7 Failed to write file
\ElkArte\Errors\Errors::instance()->log_error($_FILES['attachment']['name'][$attachID] . ': ' . $txt['php_upload_error_' . $_FILES['attachment']['error'][$attachID]], 'critical');
Errors::instance()->log_error($_FILES['attachment']['name'][$attachID] . ': ' . $txt['php_upload_error_' . $_FILES['attachment']['error'][$attachID]], 'critical');

Check warning on line 268 in sources/subs/Attachments.subs.php

View check run for this annotation

Codecov / codecov/patch

sources/subs/Attachments.subs.php#L268

Added line #L268 was not covered by tests
$errors[] = 'attach_php_error';
break;
default:
\ElkArte\Errors\Errors::instance()->log_error($_FILES['attachment']['name'][$attachID] . ': ' . $txt['php_upload_error_' . $_FILES['attachment']['error'][$attachID]]);
Errors::instance()->log_error($_FILES['attachment']['name'][$attachID] . ': ' . $txt['php_upload_error_' . $_FILES['attachment']['error'][$attachID]]);

Check warning on line 272 in sources/subs/Attachments.subs.php

View check run for this annotation

Codecov / codecov/patch

sources/subs/Attachments.subs.php#L272

Added line #L272 was not covered by tests
$errors[] = 'attach_php_error';
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/sources/UnZipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class UnZipTest extends TestCase
{
/** @var string this is a hex value for a simple zip file */
public $data = "504b0304140000000000c96d385800000000000000000000000005000000746573742f504b03041400000000000cad37580000000000000000000000000a000000746573742f746573742f504b03040a00000000000cad3758c635b93b050000000500000012000000746573742f746573742f746573742e747874746573740a504b01023f00140000000000c96d3858000000000000000000000000050024000000000000001000000000000000746573742f0a0020000000000001001800f734fcfefd4eda0100000000000000000000000000000000504b01023f001400000000000cad37580000000000000000000000000a0024000000000000001000000023000000746573742f746573742f0a002000000000000100180080ad8f0f774eda0100000000000000000000000000000000504b01023f000a00000000000cad3758c635b93b050000000500000012002400000000000000200000004b000000746573742f746573742f746573742e7478740a002000000000000100180080ad8f0f774eda0100000000000000000000000000000000504b0506000000000300030017010000800000000000";

/**
Expand Down
104 changes: 104 additions & 0 deletions tests/sources/subs/AttachmentSubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/**
*
* @package ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
*
* @version 2.0 dev
*
*/

use PHPUnit\Framework\TestCase;

class AttachmentSubs extends ElkArteCommonSetupTest
{
/**
* Prepare what is necessary to use in these tests.
*
* setUp() is run automatically by the testing framework before each test method.
*/
protected function setUp(): void
{
global $txt;

parent::setUp();
parent::setSession();

// Some strings we will need
$lang = new Loader('english', $txt, database());
$lang->load('Post');

// This is not intended to be correct, in fact it is intended to fail
$_FILES = [
'attachment' => [
'name' => ['foo.txt'],
'tmp_name' => ['/tmp/php42up23'],
'type' => ['text/plain'],
'size' => [42],
'error' => [0]
]
];

$_SESSION['temp_attachments']['post']['files'] = $_FILES;
require_once(SUBSDIR . '/Attachments.subs.php');
}

/**
* cleanup data we no longer need at the end of the tests in this class.
*
* tearDown() is run automatically by the testing framework after each test method.
*/
protected function tearDown(): void
{
parent::tearDown();

$_FILES = [];
$_SESSION['temp_attachments'] = [];
}

/**
* Just a mock to run through the attachment process code
*/
public function testProcessAttachments()
{
// Better not pass
$result = processAttachments(0);
$this->assertFalse($result);

// What did it think of this
$attach_errors = AttachmentErrorContext::context();
$result = $attach_errors->hasErrors();
$this->assertNotEmpty($result);

// Errors, we hope so
$errors = $attach_errors->prepareErrors();
$this->assertEquals('Error uploading attachments.',,$errors['attach_generic']['title']);
}

public function testGetAttachmentFromTopic()
{
$result = getAttachmentFromTopic(1,1);
$this->assertEmpty($result);
}

public function testGetUtlImageSize()
{
$result = url_image_size('https://www.elkarte.net/community/themes/default/images/logo.png)';
$this->assertEquals(145, $result[0]);
}

public function testGetServerStoredAvatars()
{
global $context, $txt, $modSettings;

$modSettings['avatar_directory'] = '/var/www/avatars';
$context['member']['avatar']['server_pic'] = 'blank.png';
$txt['no_pic'] = 'None';

$result = getServerStoredAvatars( '/Oxygen');

$this->assertEquals('beaker.png', $result[0]['files'][0]['filename']);
}
}

0 comments on commit 529fc5b

Please sign in to comment.