Skip to content

FOUR-20912: Enhance Test Coverage for Screens Functionality Before Adding Cache #7764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 138 additions & 10 deletions tests/Feature/Screens/ScreenCompiledManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature;

use Illuminate\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use ProcessMaker\Managers\ScreenCompiledManager;
Expand Down Expand Up @@ -157,27 +158,33 @@ public function it_clears_process_screens_cache()
}

/**
* Validate a screen key can be created
* Validate that a screen key can be created with various process versions, screen versions, and languages.
*
* @test
*/
public function it_creates_a_screen_key()
public function it_creates_a_screen_key_with_various_versions()
{
// Arrange
$manager = new ScreenCompiledManager();
$processId = '123';
$processVersionId = '1';
$language = 'en';
$processVersionIds = ['1', '2', '3'];
$languages = ['en', 'es', 'fr', 'de', 'it', 'pt', 'zh', 'ja', 'ru', 'ar'];
$screenId = '456';
$screenVersionId = '1';
$screenVersionIds = ['1', '2'];

$expectedKey = 'pid_123_1_en_sid_456_1';
foreach ($processVersionIds as $processVersionId) {
foreach ($screenVersionIds as $screenVersionId) {
foreach ($languages as $language) {
$expectedKey = "pid_{$processId}_{$processVersionId}_{$language}_sid_{$screenId}_{$screenVersionId}";

// Create the screen key
$screenKey = $manager->createKey($processId, $processVersionId, $language, $screenId, $screenVersionId);
// Create the screen key
$screenKey = $manager->createKey($processId, $processVersionId, $language, $screenId, $screenVersionId);

// Assert
$this->assertEquals($expectedKey, $screenKey);
// Assert
$this->assertEquals($expectedKey, $screenKey);
}
}
}
}

/**
Expand All @@ -203,4 +210,125 @@ public function it_gets_the_last_screen_version_id()
// Assert the ID is the expected one
$this->assertEquals($expectedId, $lastId);
}

/**
* Validate storing compiled content with empty content
*
* @test
*/
public function it_stores_empty_compiled_content()
{
// Arrange
$manager = new ScreenCompiledManager();
$screenKey = 'empty_screen_key';
$compiledContent = '';

// Act
$manager->storeCompiledContent($screenKey, $compiledContent);

// Assert
$filename = 'screen_' . $screenKey . '.bin';
$storagePath = $this->storagePath . $filename;

Storage::disk($this->storageDisk)->assertExists($storagePath);
$storedContent = Storage::disk($this->storageDisk)->get($storagePath);
$this->assertEquals(serialize($compiledContent), $storedContent);
}

/**
* Validate exception handling when storage is unavailable
*
* @test
*/
public function it_handles_storage_exceptions()
{
// Arrange
$manager = new ScreenCompiledManager();
$screenKey = 'exception_screen_key';
$compiledContent = ['key' => 'value'];

// Simulate storage exception
Storage::shouldReceive('disk->put')
->andThrow(new \Exception('Storage unavailable'));

// Act & Assert
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Storage unavailable');

$manager->storeCompiledContent($screenKey, $compiledContent);
}

/**
* Validate clearing compiled assets when directory does not exist
*
* @test
*/
public function it_clears_compiled_assets_when_directory_does_not_exist()
{
// Arrange
$manager = new ScreenCompiledManager();

// Ensure directory does not exist
Storage::disk($this->storageDisk)->deleteDirectory($this->storagePath);

// Act
$manager->clearCompiledAssets();

// Assert the directory has been recreated
Storage::disk($this->storageDisk)->assertExists($this->storagePath);
}

/**
* Validate that storing compiled content fails with invalid data
*
* @test
*/
public function it_fails_with_invalid_screen_key()
{
// Arrange
$manager = new ScreenCompiledManager();

// Test cases with invalid screen keys
$invalidKeys = [
'', // Empty string
null, // Null value
str_repeat('a', 1000), // Extremely long key
'../../malicious/path', // Path traversal attempt
'special@#$%chars', // Special characters
];

foreach ($invalidKeys as $invalidKey) {
try {
$manager->storeCompiledContent($invalidKey, ['test' => 'content']);
$this->fail('Expected exception was not thrown for key: ' . (string) $invalidKey);
} catch (\TypeError|\Exception $e) {
// Assert that an exception was thrown
$this->assertTrue(true);
}
}
}

/**
* Test handling of storage limit scenarios when storing compiled screen content
*
* @test
*/
public function it_handles_storage_limit_scenarios()
{
// Arrange
$manager = new ScreenCompiledManager();
$screenKey = $manager->createKey('1', '1', 'en', '1', '1');
$compiledContent = ['test' => 'content'];

// Simulate storage limit reached by throwing a specific exception
Storage::shouldReceive('disk->put')
->andThrow(new \Exception('Storage limit reached'));

// Act & Assert
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Storage limit reached');

// Attempt to store compiled content, expecting an exception
$manager->storeCompiledContent($screenKey, $compiledContent);
}
}
Loading