new class Ssettings to get videopath from db

add test - failing
remove useless videopath and tvpath from app.js
This commit is contained in:
Lukas Heiligenbrunner 2020-07-15 20:08:22 +02:00
parent 5662a6e6e5
commit b9b9ac0bc2
4 changed files with 72 additions and 15 deletions

22
api/SSettings.php Normal file
View File

@ -0,0 +1,22 @@
<?php
class SSettings
{
private $database;
/**
* SSettings constructor.
*/
public function __construct() {
$this->database = Database::getInstance();
}
public function getVideoPath() {
$query = "SELECT video_path from settings";
$result = $this->database->getConnection()->query($query);
$r = mysqli_fetch_assoc($result);
return $r['video_path'];
}
}

View File

@ -1,9 +1,15 @@
<?php <?php
require 'Database.php'; require 'Database.php';
require 'SSettings.php';
// establish initial db connection
$conn = Database::getInstance()->getConnection(); $conn = Database::getInstance()->getConnection();
$settings = new SSettings();
// load video path from settings
$videopath = $settings->getVideoPath();
//$_POST['action'] = "getRandomMovies";$_POST['number'] =6;
if (isset($_POST['action'])) { if (isset($_POST['action'])) {
$action = $_POST['action']; $action = $_POST['action'];
switch ($action) { switch ($action) {
@ -84,7 +90,9 @@ if (isset($_POST['action'])) {
$arr["movie_id"] = $row["movie_id"]; $arr["movie_id"] = $row["movie_id"];
$arr["movie_name"] = $row["movie_name"]; $arr["movie_name"] = $row["movie_name"];
$arr["movie_url"] = str_replace("?","%3F",$row["movie_url"]); // todo drop video url from db -- maybe one with and one without extension
// extension hardcoded here!!!
$arr["movie_url"] = str_replace("?", "%3F", $videopath . $row["movie_name"] . ".mp4");
$arr["likes"] = $row["likes"]; $arr["likes"] = $row["likes"];
$arr["quality"] = $row["quality"]; $arr["quality"] = $row["quality"];
$arr["length"] = $row["length"]; $arr["length"] = $row["length"];

View File

@ -14,7 +14,8 @@ class App extends React.Component {
this.state = { this.state = {
page: "default", page: "default",
generalSettingsLoaded: false, generalSettingsLoaded: false,
passwordsupport: null passwordsupport: null,
mediacentername: "OpenMediaCenter"
}; };
// bind this to the method for being able to call methods such as this.setstate // bind this to the method for being able to call methods such as this.setstate
@ -22,8 +23,6 @@ class App extends React.Component {
this.returnToLastElement = this.returnToLastElement.bind(this); this.returnToLastElement = this.returnToLastElement.bind(this);
} }
generaldata = {};
componentDidMount() { componentDidMount() {
const updateRequest = new FormData(); const updateRequest = new FormData();
updateRequest.append('action', 'loadInitialData'); updateRequest.append('action', 'loadInitialData');
@ -31,16 +30,13 @@ class App extends React.Component {
fetch('/api/Settings.php', {method: 'POST', body: updateRequest}) fetch('/api/Settings.php', {method: 'POST', body: updateRequest})
.then((response) => response.json() .then((response) => response.json()
.then((result) => { .then((result) => {
this.generaldata = { console.log(result);
videopath: result.video_path,
tvshowpath: result.episode_path,
mediacentername: result.mediacenter_name
};
this.setState({ this.setState({
generalSettingsLoaded: true, generalSettingsLoaded: true,
passwordsupport: result.passwordEnabled passwordsupport: result.passwordEnabled,
mediacentername: result.mediacenter_name
}); });
console.log(this.state);
})); }));
} }
@ -49,8 +45,7 @@ class App extends React.Component {
constructViewBinding(){ constructViewBinding(){
return { return {
changeRootElement: this.changeRootElement, changeRootElement: this.changeRootElement,
returnToLastElement: this.returnToLastElement, returnToLastElement: this.returnToLastElement
generalsettings: this.generaldata
}; };
} }
@ -86,7 +81,7 @@ class App extends React.Component {
return ( return (
<div className="App"> <div className="App">
<nav className="navbar navbar-expand-sm bg-primary navbar-dark"> <nav className="navbar navbar-expand-sm bg-primary navbar-dark">
<div className="navbar-brand">OpenMediaCenter</div> <div className="navbar-brand">{this.state.mediacentername}</div>
<ul className="navbar-nav"> <ul className="navbar-nav">
<li className="nav-item"> <li className="nav-item">

View File

@ -2,6 +2,14 @@ import React from 'react';
import App from './App'; import App from './App';
import {shallow} from 'enzyme' import {shallow} from 'enzyme'
function prepareFetchApi(response) {
const mockJsonPromise = Promise.resolve(response);
const mockFetchPromise = Promise.resolve({
json: () => mockJsonPromise,
});
return (jest.fn().mockImplementation(() => mockFetchPromise));
}
describe('<App/>', function () { describe('<App/>', function () {
it('renders without crashing ', function () { it('renders without crashing ', function () {
const wrapper = shallow(<App/>); const wrapper = shallow(<App/>);
@ -82,4 +90,28 @@ describe('<App/>', function () {
wrapper.find(".nav-link").findWhere(t => t.text() === "Settings" && t.type() === "div").simulate("click"); wrapper.find(".nav-link").findWhere(t => t.text() === "Settings" && t.type() === "div").simulate("click");
expect(wrapper.find("SettingsPage")).toHaveLength(1); expect(wrapper.find("SettingsPage")).toHaveLength(1);
}); });
it('test initial fetch from api', done => {
global.fetch = prepareFetchApi({
generalSettingsLoaded: true,
passwordsupport: true,
mediacentername: "testname"
});
const wrapper = shallow(<App/>);
expect(global.fetch).toBeCalledTimes(1);
process.nextTick(() => {
console.log(wrapper.state());
// todo state lifecycle not triggered
wrapper.update();
console.log(wrapper.state());
expect(wrapper.state('passwordsupport')).toBe(true);
global.fetch.mockClear();
done();
});
});
}); });