<commit_before><?php
/**
 * @group Form_File
 */
class Vps_Form_File_FileTest extends PHPUnit_Framework_TestCase
{
    private $_model;
    private $_uploadsModel;
    private $_row;
    private $_field;

    public function setUp()
    {
        Vps_Model_Abstract::clearInstances();
        $this->_uploadsModel = Vps_Model_Abstract::getInstance('Vps_Form_File_UploadsTestModel');

        $this->_model = new Vps_Model_FnF(array(
            'referenceMap' => array('File' => array(
                'refModelClass' => 'Vps_Form_File_UploadsTestModel',
                'column' => 'upload_id'
            )),
            'data' => array(
            )
        ));
        $fRow = $this->_uploadsModel->createRow()->writeFile('asdf', 'foo', 'txt');
        $this->_uploadsModel->createRow()->writeFile('asdf1', 'foo1', 'txt');

        $this->_row = $this->_model->createRow();
        $this->_row->upload_id = $fRow->id;
        $this->_row->save();
        $this->_field = new Vps_Form_Field_File('File');
    }
    public function tearDown()
    {
        $dir = $this->_uploadsModel->getUploadDir();
        if (substr($dir, 0, 4)=='/tmp') {
            system('rm -r '.$dir);
        }
    }

    public function testProcessInputNothingUploaded()
    {
        $input = array(
            'File_upload_id' => '1'
        );
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertEquals($data, array('File' => '1'));

        $input = array(
            'File_upload_id' => '1',
            'File' => array(
                'error' => UPLOAD_ERR_NO_FILE,
                'tmp_name' => false
            )
        );
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertEquals($data, array('File' => '1'));
    }

    public function testProcessInputNothingUploadedEmpty()
    {
        $input = array(
            'File_upload_id' => ''
        );
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertSame($data, array('File' => null));
    }

    public function testProcessInputEmpty()
    {
        $input = array();
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertSame($data, array());
    }

    public function testProcessInputFileUploaded()
    {
        $file = tempnam('/tmp', 'uploadtest');
        file_put_contents('foo', $file);
        $input = array(
            'File_upload_id' => '1',
            'File' => array(
                'error' => 0,
                'tmp_name' => $file,
                'type' => 'plain/text',
                'name' => 'test.txt'
            )
        );
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertEquals($data, array('File' => '3'));
    }

    public function testProcessInputFileUploadedNothingExisting()
    {
        $file = tempnam('/tmp', 'uploadtest');
        file_put_contents('foo', $file);
        $input = array(
            'File' => array(
                'error' => 0,
                'tmp_name' => $file,
                'type' => 'plain/text',
                'name' => 'test.txt'
            )
        );
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertEquals($data, array('File' => '3'));
    }

    public function testProcessInputFileDelete()
    {
        $input = array(
            'File_upload_id' => '1',
            'File_del' => '1',
        );
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertSame($data, array('File' => null));
    }

    public function testProcessInputFileDeleteAndUploaded()
    {
        $file = tempnam('/tmp', 'uploadtest');
        file_put_contents('foo', $file);

        $input = array(
            'File_upload_id' => '1',
            'File_del' => '1',
            'File' => array(
                'error' => 0,
                'tmp_name' => $file,
                'type' => 'plain/text',
                'name' => 'test.txt'
            )
        );
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertSame($data, array('File' => null));
    }

    public function testProcessInputBackend()
    {
        $input = array(
            'File' => '1',<commit_msg>Kl. fix in Tests bei file_put_contents - vertauschte argumente :)<commit_after><?php
/**
 * @group Form_File
 */
class Vps_Form_File_FileTest extends PHPUnit_Framework_TestCase
{
    private $_model;
    private $_uploadsModel;
    private $_row;
    private $_field;

    public function setUp()
    {
        Vps_Model_Abstract::clearInstances();
        $this->_uploadsModel = Vps_Model_Abstract::getInstance('Vps_Form_File_UploadsTestModel');

        $this->_model = new Vps_Model_FnF(array(
            'referenceMap' => array('File' => array(
                'refModelClass' => 'Vps_Form_File_UploadsTestModel',
                'column' => 'upload_id'
            )),
            'data' => array(
            )
        ));
        $fRow = $this->_uploadsModel->createRow()->writeFile('asdf', 'foo', 'txt');
        $this->_uploadsModel->createRow()->writeFile('asdf1', 'foo1', 'txt');

        $this->_row = $this->_model->createRow();
        $this->_row->upload_id = $fRow->id;
        $this->_row->save();
        $this->_field = new Vps_Form_Field_File('File');
    }
    public function tearDown()
    {
        $dir = $this->_uploadsModel->getUploadDir();
        if (substr($dir, 0, 4)=='/tmp') {
            system('rm -r '.$dir);
        }
    }

    public function testProcessInputNothingUploaded()
    {
        $input = array(
            'File_upload_id' => '1'
        );
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertEquals($data, array('File' => '1'));

        $input = array(
            'File_upload_id' => '1',
            'File' => array(
                'error' => UPLOAD_ERR_NO_FILE,
                'tmp_name' => false
            )
        );
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertEquals($data, array('File' => '1'));
    }

    public function testProcessInputNothingUploadedEmpty()
    {
        $input = array(
            'File_upload_id' => ''
        );
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertSame($data, array('File' => null));
    }

    public function testProcessInputEmpty()
    {
        $input = array();
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertSame($data, array());
    }

    public function testProcessInputFileUploaded()
    {
        $file = tempnam('/tmp', 'uploadtest');
        file_put_contents($file, 'foo');
        $input = array(
            'File_upload_id' => '1',
            'File' => array(
                'error' => 0,
                'tmp_name' => $file,
                'type' => 'plain/text',
                'name' => 'test.txt'
            )
        );
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertEquals($data, array('File' => '3'));
    }

    public function testProcessInputFileUploadedNothingExisting()
    {
        $file = tempnam('/tmp', 'uploadtest');
        file_put_contents($file, 'foo');
        $input = array(
            'File' => array(
                'error' => 0,
                'tmp_name' => $file,
                'type' => 'plain/text',
                'name' => 'test.txt'
            )
        );
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertEquals($data, array('File' => '3'));
    }

    public function testProcessInputFileDelete()
    {
        $input = array(
            'File_upload_id' => '1',
            'File_del' => '1',
        );
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertSame($data, array('File' => null));
    }

    public function testProcessInputFileDeleteAndUploaded()
    {
        $file = tempnam('/tmp', 'uploadtest');
        file_put_contents($file, 'foo');

        $input = array(
            'File_upload_id' => '1',
            'File_del' => '1',
            'File' => array(
                'error' => 0,
                'tmp_name' => $file,
                'type' => 'plain/text',
                'name' => 'test.txt'
            )
        );
        $data = $this->_field->processInput($this->_row, $input);
        $this->assertSame($data, array('File' => null));
    }

    public function testProcessInputBackend()
    {
        $input = array(
            'File' => '1',
{'fraction_non_alphanumeric': 0.17171481826654242, 'fraction_numerical': 0.004426840633737186, 'mean_word_length': 1.9240463215258856, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 183, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5195156', 'n_tokens_mistral': 2622, 'n_tokens_neox': 2545, 'n_words': 441}