254 lines
No EOL
9.3 KiB
PHP
254 lines
No EOL
9.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\BusinessPlan;
|
|
|
|
use Tests\TestCase;
|
|
use App\Services\BusinessPlan\TreeHtmlRenderer;
|
|
use App\Services\BusinessPlan\BusinessUserItemOptimized;
|
|
use stdClass;
|
|
|
|
class TreeHtmlRendererTest extends TestCase
|
|
{
|
|
private $renderer;
|
|
private $dateObj;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->renderer = new TreeHtmlRenderer('admin');
|
|
|
|
$this->dateObj = new stdClass();
|
|
$this->dateObj->month = 1;
|
|
$this->dateObj->year = 2024;
|
|
$this->dateObj->start_date = '2024-01-01 00:00:00';
|
|
$this->dateObj->end_date = '2024-01-31 23:59:59';
|
|
}
|
|
|
|
/** @test */
|
|
public function constructor_sets_init_from_correctly()
|
|
{
|
|
$adminRenderer = new TreeHtmlRenderer('admin');
|
|
$memberRenderer = new TreeHtmlRenderer('member');
|
|
$defaultRenderer = new TreeHtmlRenderer();
|
|
|
|
// Using reflection to access private property
|
|
$reflection = new \ReflectionClass(TreeHtmlRenderer::class);
|
|
$property = $reflection->getProperty('initFrom');
|
|
$property->setAccessible(true);
|
|
|
|
$this->assertEquals('admin', $property->getValue($adminRenderer));
|
|
$this->assertEquals('member', $property->getValue($memberRenderer));
|
|
$this->assertEquals('member', $property->getValue($defaultRenderer));
|
|
}
|
|
|
|
/** @test */
|
|
public function renderTree_returns_info_message_for_empty_array()
|
|
{
|
|
$result = $this->renderer->renderTree([]);
|
|
|
|
$this->assertStringContainsString('Keine Business-User gefunden', $result);
|
|
$this->assertStringContainsString('alert-info', $result);
|
|
}
|
|
|
|
/** @test */
|
|
public function renderTree_generates_html_structure_for_business_users()
|
|
{
|
|
// Create mock business user items
|
|
$businessUsers = [
|
|
$this->createMockBusinessUser(1, 'John', 'Doe', 'john@example.com', true),
|
|
$this->createMockBusinessUser(2, 'Jane', 'Smith', 'jane@example.com', false)
|
|
];
|
|
|
|
$result = $this->renderer->renderTree($businessUsers);
|
|
|
|
// Check basic HTML structure
|
|
$this->assertStringContainsString('<ol class="dd-list">', $result);
|
|
$this->assertStringContainsString('</ol>', $result);
|
|
$this->assertStringContainsString('dd-item', $result);
|
|
|
|
// Check user data is included
|
|
$this->assertStringContainsString('John Doe', $result);
|
|
$this->assertStringContainsString('Jane Smith', $result);
|
|
$this->assertStringContainsString('john@example.com', $result);
|
|
$this->assertStringContainsString('jane@example.com', $result);
|
|
}
|
|
|
|
/** @test */
|
|
public function renderTree_includes_admin_buttons_for_admin_init()
|
|
{
|
|
$adminRenderer = new TreeHtmlRenderer('admin');
|
|
$businessUsers = [
|
|
$this->createMockBusinessUser(1, 'John', 'Doe', 'john@example.com', true)
|
|
];
|
|
|
|
$result = $adminRenderer->renderTree($businessUsers);
|
|
|
|
// Should include calculator button for admin
|
|
$this->assertStringContainsString('fa-calculator', $result);
|
|
$this->assertStringContainsString('business-user-detail', $result);
|
|
}
|
|
|
|
/** @test */
|
|
public function renderTree_excludes_admin_buttons_for_member_init()
|
|
{
|
|
$memberRenderer = new TreeHtmlRenderer('member');
|
|
$businessUsers = [
|
|
$this->createMockBusinessUser(1, 'John', 'Doe', 'john@example.com', true)
|
|
];
|
|
|
|
$result = $memberRenderer->renderTree($businessUsers);
|
|
|
|
// Should not include calculator button for regular members
|
|
$this->assertStringNotContainsString('fa-calculator', $result);
|
|
}
|
|
|
|
/** @test */
|
|
public function renderTree_handles_inactive_accounts_correctly()
|
|
{
|
|
$businessUsers = [
|
|
$this->createMockBusinessUser(1, 'Active', 'User', 'active@example.com', true),
|
|
$this->createMockBusinessUser(2, 'Inactive', 'User', 'inactive@example.com', false)
|
|
];
|
|
|
|
$result = $this->renderer->renderTree($businessUsers);
|
|
|
|
// Active user should have primary color icon
|
|
$this->assertStringContainsString('text-primary', $result);
|
|
// Inactive user should have danger/muted styling
|
|
$this->assertStringContainsString('text-danger', $result);
|
|
$this->assertStringContainsString('text-muted', $result);
|
|
}
|
|
|
|
/** @test */
|
|
public function renderParentless_returns_empty_message_for_empty_array()
|
|
{
|
|
$result = $this->renderer->renderParentless([]);
|
|
|
|
$this->assertStringContainsString('Keine parentlosen User gefunden', $result);
|
|
$this->assertStringContainsString('alert-info', $result);
|
|
}
|
|
|
|
/** @test */
|
|
public function renderParentless_generates_list_for_parentless_users()
|
|
{
|
|
$parentlessUsers = [
|
|
$this->createMockBusinessUser(1, 'Orphan', 'User', 'orphan@example.com', true)
|
|
];
|
|
|
|
$result = $this->renderer->renderParentless($parentlessUsers);
|
|
|
|
$this->assertStringContainsString('dd-item', $result);
|
|
$this->assertStringContainsString('Orphan User', $result);
|
|
$this->assertStringContainsString('orphan@example.com', $result);
|
|
}
|
|
|
|
/** @test */
|
|
public function renderSponsor_returns_no_sponsor_message_for_null()
|
|
{
|
|
$result = $this->renderer->renderSponsor(null);
|
|
|
|
$this->assertStringContainsString('alert-warning', $result);
|
|
$this->assertStringContainsString('team.no_sponsor_assigned', $result);
|
|
}
|
|
|
|
/** @test */
|
|
public function renderSponsor_generates_sponsor_html()
|
|
{
|
|
$sponsor = $this->createMockBusinessUser(1, 'Sponsor', 'User', 'sponsor@example.com', true);
|
|
|
|
$result = $this->renderer->renderSponsor($sponsor);
|
|
|
|
$this->assertStringContainsString('dd-item', $result);
|
|
$this->assertStringContainsString('Sponsor User', $result);
|
|
$this->assertStringContainsString('sponsor@example.com', $result);
|
|
}
|
|
|
|
/** @test */
|
|
public function renderSponsor_includes_admin_details_for_admin_init()
|
|
{
|
|
$adminRenderer = new TreeHtmlRenderer('admin');
|
|
$sponsor = $this->createMockBusinessUser(1, 'Sponsor', 'User', 'sponsor@example.com', true);
|
|
|
|
// Set some business data
|
|
$sponsor->sales_volume_points_KP_sum = 1000;
|
|
$sponsor->sales_volume_total_sum = 5000;
|
|
|
|
$result = $adminRenderer->renderSponsor($sponsor);
|
|
|
|
$this->assertStringContainsString('1000', $result);
|
|
$this->assertStringContainsString('5000', $result);
|
|
$this->assertStringContainsString('total_points', $result);
|
|
}
|
|
|
|
/** @test */
|
|
public function renderItem_handles_deep_nesting()
|
|
{
|
|
// Create business user with nested children
|
|
$parentUser = $this->createMockBusinessUser(1, 'Parent', 'User', 'parent@example.com', true);
|
|
$childUser = $this->createMockBusinessUser(2, 'Child', 'User', 'child@example.com', true);
|
|
|
|
// Set up nesting
|
|
$parentUser->businessUserItems = [$childUser];
|
|
|
|
$result = $this->renderer->renderTree([$parentUser]);
|
|
|
|
// Should contain both parent and child
|
|
$this->assertStringContainsString('Parent User', $result);
|
|
$this->assertStringContainsString('Child User', $result);
|
|
|
|
// Should have nested structure
|
|
$this->assertStringContainsString('<ol class="dd-list dd-nodrag">', $result);
|
|
}
|
|
|
|
/** @test */
|
|
public function html_output_is_properly_escaped()
|
|
{
|
|
$businessUsers = [
|
|
$this->createMockBusinessUser(1, 'John <script>', 'Doe & Co', 'john@example.com', true)
|
|
];
|
|
|
|
$result = $this->renderer->renderTree($businessUsers);
|
|
|
|
// HTML should be escaped
|
|
$this->assertStringNotContainsString('<script>', $result);
|
|
$this->assertStringContainsString('<script>', $result);
|
|
$this->assertStringContainsString('&', $result);
|
|
}
|
|
|
|
/**
|
|
* Helper method to create mock business user
|
|
*/
|
|
private function createMockBusinessUser($id, $firstName, $lastName, $email, $activeAccount)
|
|
{
|
|
$businessUser = new BusinessUserItemOptimized($this->dateObj);
|
|
|
|
// Create a mock b_user object
|
|
$bUser = new stdClass();
|
|
$bUser->user_id = $id;
|
|
$bUser->first_name = $firstName;
|
|
$bUser->last_name = $lastName;
|
|
$bUser->email = $email;
|
|
$bUser->active_account = $activeAccount;
|
|
$bUser->user_level_name = 'Bronze';
|
|
$bUser->m_account = 'TEST' . $id;
|
|
$bUser->user_birthday = '1990-01-01';
|
|
$bUser->user_phone = '+1234567890';
|
|
$bUser->sales_volume_points_KP_sum = 100;
|
|
$bUser->sales_volume_KP_points = 50;
|
|
$bUser->sales_volume_points_shop = 50;
|
|
$bUser->sales_volume_total_sum = 1000;
|
|
$bUser->sales_volume_total = 500;
|
|
$bUser->sales_volume_total_shop = 500;
|
|
$bUser->payment_account_date = '2024-01-15';
|
|
$bUser->businessUserItems = [];
|
|
|
|
// Use reflection to set private property
|
|
$reflection = new \ReflectionClass($businessUser);
|
|
$property = $reflection->getProperty('b_user');
|
|
$property->setAccessible(true);
|
|
$property->setValue($businessUser, $bUser);
|
|
|
|
return $businessUser;
|
|
}
|
|
} |