Skip to content

Commit

Permalink
Merge pull request #20 from grnhse/fix_uploaded_file_bug
Browse files Browse the repository at this point in the history
Allow for required files to correctly check for URLs and content.
  • Loading branch information
tdphillipsjr authored Feb 24, 2020
2 parents 5744b07 + 7d660ab commit 33c3d0c
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Services/ApplicationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,50 @@ public function hasRequiredValue($postVars, $keys)
$requiredKey = $this->findKey($key, $postVars);
if (array_key_exists($requiredKey, $postVars) && $postVars[$requiredKey] !== '') return true;
}

if ($this->_hasRequiredValuesForFiles($postVars, $key)) return true;

return false;
}

/**
* Previously, Greenhouse only accepted files in one way via resume and resume_text. Since, they
* included the ability to send files by linking to them or via binary content. However, they still
* only mark the resume as required using the 'resume' field in the jobs description. This method also
* checks the alternatives. The alternatives are:
* <field>_content and <field>_content_filename
* <field>_url and <field>_url_filename
* If either of these are included, the requirement should pass. This is the case for resume
* and cover_letter; and also the case for custom questions which may also contain a file.
*
* @params Array $postVars Greenhouse post parameters
* @params string $key The key we are validating
* @return boolean
*/
private function _hasRequiredValuesForFiles($postVars, $key)
{
$contentKey = "${key}_content";
$contentFilenameKey = "${key}_content_filename";
$urlKey = "${key}_url";
$urlFilenameKey = "${key}_url_filename";

if (array_key_exists($contentKey, $postVars) &&
array_key_exists($contentFilenameKey, $postVars) &&
$postVars[$contentKey] !== '' &&
$postVars[$contentFilenameKey] !== '') {
return true;
}

if (array_key_exists($urlKey, $postVars) &&
array_key_exists($urlFilenameKey, $postVars) &&
$postVars[$urlKey] !== '' &&
$postVars[$urlFilenameKey] !== '') {
return true;
}

return false;
}

/**
* This fixes an issue where the Greenhouse API returns required multiselects as <field[]> but we require
* the user to submit it as <field>, thus making the system think the required field is not set. This
Expand Down
80 changes: 80 additions & 0 deletions tests/Services/ApplicationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,86 @@ public function testHasRequiredValueMultipleZero()

$this->assertTrue($this->appService->hasRequiredValue($postVars, $keys));
}

public function testHasBinaryFilenameFieldPass()
{
$postVars = array('foo' => '', 'foo_content' => 'abce', 'foo_content_filename' => 'test.pdf');
$keys = array('foo');

$this->assertTrue($this->appService->hasRequiredValue($postVars, $keys));
}

public function testHasBinaryFilenameFieldFailContentBlank()
{
$postVars = array('foo' => '', 'foo_content' => '', 'foo_content_filename' => 'test.pdf');
$keys = array('foo');

$this->assertFalse($this->appService->hasRequiredValue($postVars, $keys));
}

public function testHasBinaryFilenameFieldFailContentNotIncluded()
{
$postVars = array('foo' => '', 'foo_content_filename' => 'test.pdf');
$keys = array('foo');

$this->assertFalse($this->appService->hasRequiredValue($postVars, $keys));
}

public function testHasBinaryFilenameFieldFailFilenameBlank()
{
$postVars = array('foo' => '', 'foo_content' => 'abce', 'foo_content_filename' => '');
$keys = array('foo');

$this->assertFalse($this->appService->hasRequiredValue($postVars, $keys));
}

public function testHasBinaryFilenameFieldFailFilenameNotIncluded()
{
$postVars = array('foo' => '', 'foo_content' => 'abcde');
$keys = array('foo');

$this->assertFalse($this->appService->hasRequiredValue($postVars, $keys));
}

public function testHasUrlFilenameFieldPass()
{
$postVars = array('foo' => '', 'foo_url' => 'http://t.co', 'foo_url_filename' => 'test.pdf');
$keys = array('foo');

$this->assertTrue($this->appService->hasRequiredValue($postVars, $keys));
}

public function testHasUrlFilenameFieldFailUrlBlank()
{
$postVars = array('foo' => '', 'foo_url' => '', 'foo_url_filename' => 'test.pdf');
$keys = array('foo');

$this->assertFalse($this->appService->hasRequiredValue($postVars, $keys));
}

public function testHasUrlFilenameFieldFailUrlFilenameBlank()
{
$postVars = array('foo' => '', 'foo_url' => 'http://t.co', 'foo_url_filename' => '');
$keys = array('foo');

$this->assertFalse($this->appService->hasRequiredValue($postVars, $keys));
}

public function testHasUrlFilenameFieldFailUrlNotIncluded()
{
$postVars = array('foo' => '', 'foo_url_filename' => 'test.pdf');
$keys = array('foo');

$this->assertFalse($this->appService->hasRequiredValue($postVars, $keys));
}

public function testHasUrlFilenameFieldFailUrlFilenameNotIncluded()
{
$postVars = array('foo' => '', 'foo_url' => 'http://t.co');
$keys = array('foo');

$this->assertFalse($this->appService->hasRequiredValue($postVars, $keys));
}

public function testGetRequiredFields()
{
Expand Down

0 comments on commit 33c3d0c

Please sign in to comment.